I'm trying to write a program to experiment with the fgets() function, with which I have little experience. I keep getting a segmentation fault 11 error when I run the program (but no errors when I compile it) and I don't know enough about fgets to know what is going on. Here is my program:
#include <stdio.h>
int main (void) {
FILE *fp;
fp = fopen("wageData.txt","r+");
char x[3][5];
int i = 0;
while (i < 3) {
fgets(x[i], 4, fp);
i++;
}
for (i = 0; i < 3; i++) {
printf("%s\n", x[i]);
}
return 0;
}
Here is the text file that I have linked to it:
Hi, my name is Frank. I like pie. My car is black.
fopen()
. Also, the size ofx[i]
is 5, you should havefgets(x[i], 5, fp)
, or better yet,fgets(x[i], sizeof(x[i]), fp)
. – Filipe Gonçalvesprintf()
s to get more intormation – Ingo Leonhardtfgets
failsif (NULL==fgets(...)) {...}
– Weather Vane