Jump to content
  • Advertisement
Sign in to follow this  
deavik

fgets() question

This topic is 4797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
Or:
while (fgets(buf, 255, fin))
{
// do stuff
}
// check error condition using ferror() or feof()


Enigma

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!