Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

reading a file and detecting the 'end of line'

This topic is 4087 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 all. i am reading this file using iostreams and i am having problems detecting the end of line AND end of file.. evidently end of file checks are not good enough. without going into a lot of detail as to what i am doing i hope you can see from the code what may help remedy this:
while(!input_file.eof())
 { 
  for(int i = 0; i < 9; i++)
   {
    input_file.getline(key, MAX_LENGTH, ":");
    char temp[MAX_LENGTH];
    input_file.getline(temp, MAX_LENGTH);
    names temp; //char to a string
   }
  }
the while check doesnt seem to catch the EOL character. how can i make sure that i am not only EOF but also EOL at the same time?? thanks!

Share this post


Link to post
Share on other sites
Advertisement
You want to use getline():


for( size_t i = 0; i < SIZE && getline( input_file, names ); ++i );


Where SIZE is the size of names. What is names, by the way?

jfl.

Share this post


Link to post
Share on other sites
1) Don't nest the loops like that; you want to loop while you can get a line *and* there is room in the buffer, so there's one loop, and it has that combined condition.

2) Don't use .eof() for looping. It doesn't report true until a read has *already failed*. Instead, use the read operation itself in the loop condition.

3) Don't use the member function .getline() of iostreams. Read directly into std::strings with the free function std::getline() instead. This is considered by many a wart on the standard library design, but it's worth living with (just imagine the alternative of implementing these things yourself!).


for (int i = 0;
i < 9 && getline(input_file, key, ":") && getline(input_file, names);
++i) {} // nothing to do in the loop body.


4) If the limit of 9 strings happens to be arbitrary, then don't settle for arbitrary limits in C++: use std::vector instead -


vector<string> names;
string key, temp;
while (getline(input_file, key, ":") && getline(input_file, temp)) {
names.push_back(temp);
}

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!