Code:
while( feof(fileptr) != EOF ) {
if( fgets(string, 30, fileptr) == NULL ) {
return 0;
}
printf("%s", string);
}
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. In addition, using feof() to control a loop is usually wrong. The following loop can't be used as a general drop-in for the above loop, but the above loop's "return 0" is a pretty special case that I think, generally, would be replaced with "break", so as an improved loop:
Code:
while(fgets(string, sizeof string, fileptr) != NULL)
{
printf("%s", string);
}
Next:
Code:
if( string[strlen(string)] == '\0' ) {
printf(" Newline\n");
}
string[strlen(string)] is always 0 (which is the same as '\0'), or undefined. The above test is therefore not useful. strlen() gives the length of a string, minus its terminating null character (which all strings must have). So:
Code:
char s[] = "hi";
size_t n = strlen(s); /* This is 2 */
/* s[0] is 'h', s[1] is 'i', and s[2] is 0 */