I am trying to read from a text file and this is my file reader:
int readFile(char strArray[MAX][MAX]) {
FILE *infile;
infile = fopen("input.txt", "r");
int row = 0;
if(infile == NULL)
exit(0);
while(feof(infile) == 0) {
fgets(strArray[row], 100, infile);
row++;
}
fclose(infile);
return row;
}
and the content of the "input.txt" file I was trying to read:
hello
goodbye
**an empty line***
The problem occurred when I left the last line of the text file as empty, my program prints out undefined symbols. I have fixed this issue by removing the last empty line in the text file.
But I am wondering if there is a way to leave the empty line at the bottom of the text file ?
I have tried the solution from this stackoverflow post but it didn't work and it also froze my complier.
I know that I should not try using feof()
from this stackoverflow post so should I use fgets()
to check the characters from my loop and I would love to know some more other alternative ways?