I want to read a text file into a string array and be able to access the array contents through a loop. The code I have allows me to store only the last line of the text file instead of the entire file; where am I going wrong?
#define MAX 10000
int main (int argc, char *argv[])
{
FILE *fp;
char str[MAX];
char *x[MAX];
int i =0;
char y[MAX];
if((fp = fopen("550.txt", "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);}
while(!feof(fp)) {
while(fgets(str, sizeof str, fp)) {
x[i]= str;
printf("%s", str);
printf("%s", *(x+i));
i++;
}
}
for(i=0;i<100;i++){
printf("%s", *(x+i));
}
fclose(fp);
return 0;
}