I wanna store an array of Strings and display it like this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
int i = 0;
char* array[200000];
char prod [10];
FILE * fp = fopen ("arrayValues.txt", "r");
while (fgets(prod, 10, fp) != NULL) {
array[i] = strtok(prod, "\n\r");
i++;
}
fclose(fp);
for (i = 0; array[i] ; i++) {
printf("%s %d\n", array[i], i);
}
}
but the output is only the last line of the file im working with x times. Suggestions?
\n\r. Windows uses\r\nand *nix use\narrayvalues.txtlook like? I assume you are trying to load each line in the array.prodevery time you read a line, not making a copy. Andstrtok()just returns a pointer into the string. So all your array elements point to that same string that's being overwritten.prodand modify the contents ofprod. you are always storing prod to array[i]. you are also modifying the contents ofprodevery time inside your while loop via fgets. thus all the prints will be the same. also, you don't terminate your last array value with NULL to indicate the end.strdup(strtok(...))And initialize the array likechar* array[200000] = {0}