Originally Posted by
cas
feof() does not return EOF; or, at least, it is specificed as returning zero or non-zero, so it makes no sense to compare against EOF
EOF is defined as -1 (in stdio.h), which is nonzero and aids in the reading of the code.
Originally Posted by
cas
string[strlen(string)] is always 0 (which is the same as '\0'), or undefined.
That's true, apologies it was added in a rush. The use of feof is fine there, though removing it and following your example is more efficient. The loop looking like this now:
Code:
#include <stdio.h>
#include <string.h>
int main( void ) {
short maxSize = 30;
char string[maxSize];
FILE *fileptr = fopen("example.txt","r");
while( fgets(string, maxSize, fileptr) != NULL ) {
printf("%s", string);
if( strlen(string) != maxSize - 1 ) {
printf(" Newline\n");
}
}
return 0;
}
Note that the size of the string array is set in a variable, the printf when the end of the string is detected isn't required, and blank lines, and the end-of-file also meet the conditions of the newline if statement.
Apologies to Raskalnikov for the mess.