testing for EOF with strings
if the return value of EOF is an int, then how can you test for it with strings? I currently have a pointer to the string and I use it to test chars. When I try setting up a loop to test for the EOF at the end of a text file and compile I get an infinite loop with puts only printing out the last word over and over. what am I doing wrong?
Thanx
Doug
Re: testing for EOF with strings
Quote:
Originally posted by Unregistered
if the return value of EOF is an int, then how can you test for it with strings?
This doesn't seem to make much sense to me, but I think I might have an idea what the problem is.
EOF is a macro which is a value of type int. It is returned by many of the C file IO functions, but is not the only way in which end-of-file is detected. Functions which return char pointers (like fgets) do NOT return EOF on end-of-file - they return a NULL pointer.
Quote:
I currently have a pointer to the string and I use it to test chars. When I try setting up a loop to test for the EOF at the end of a text file and compile I get an infinite loop with puts only printing out the last word over and over. what am I doing wrong?
Compare what you're doing with this proglet:
#include <stdio.h>
#include <stdlib.h>
#define LINE_LEN (2048)
int main(void) {
FILE * foo;
char s[LINE_LEN];
char * u;
foo=fopen("myfile.txt","r");
if (!foo) return EXIT_FAILURE;
do {
u=fgets(s,LINE_LEN,foo);
if (u==NULL) break; /* end of file detected! */
} while (u);
fclose(foo);
return EXIT_SUCCESS;
}
/* warning, untested code... but it should do! */