Sign in to follow this  
deavik

fgets() question

Recommended Posts

deavik    570
Hi, I am trying to read in data from a file line by line with fgets(). I read some doc which said that fgets returns a NULL pointer at end of file, but it didn't work for me. I used a while loop: FILE *fin; fin = fopen (filename, "rt"); char *buf[255]; while(1) { fgets (buf, 255, fin); if (buf == NULL) break; // .... } but i just got an infinite loop crashing my program. So how do I know when I have reached end of file? I don't want to use fstream functions. Thanks

Share this post


Link to post
Share on other sites
Lajnold    226
Using feof() to control the loop might be a bad idea. Why that is, you can read here http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351

Share this post


Link to post
Share on other sites
deavik    570
I hear what you're saying, and I tried numerous other methods like what enigma said, or

int c;

while ((c = fgetc(fin)) != EOF)
{
// ...
}

but I am getting unexpected results - like (I am actually reading in a list of vertices and faces for drawing with opengl, and) all the faces aren't being drawn. There are no blank lines in the file, either.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this