Really struggling here and cannot figure out how to get my value's from an array.
I first declare this array which I want to hold a set of numbers. IDK why the size is 64, I simply am frustrated and gave it a size.
char *numberList1[64];
I then read in a file using some code I found.
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
char string[100];
int counter = 0;
printf("\n\nEnter name of File 1: ");
scanf("%s", string);
fp = fopen(string, "r");
if (fp == NULL) {
printf("invalid filename of %s",string);
exit(EXIT_FAILURE);
}
while ((read = getline(&line, &len, fp)) != -1) {
numberList1[counter] = line;
counter++;
}
Now if were to say something like printf("%s",numberList1[counter]); within the while, I would get all my numbers back.
However when I say the following, I only get the last item printed out however times there are numbers. (vectorLength).
int j;
//vectorLength is the user-entered # of lines/numbers in the file.
for (j = 0; j < vectorLength; j++) {
printf("Writing out: %s \n", numberList1[j] );
//write(pipe1[WRITE], currentNum, bitSize+1);
}
i.e. If I had numbers, 1, 2, 3, 4 I would get: 4 4 4 4
What am I doing wrong??? I have tried to find guides on understanding pointers and arrays in C but I cannot figure it out...
vectorLength? What is it's value?