0

does anyone know any easy way of reading in a file line by line.
im assuming i need to use ifstream
then just do while (!file.eof()) getline(file, string);
i need to read each string into a switch statement and then extract 3 values: vertex.x vertex.y vertex.z for each string read in depending on the case statements. can anyone shed some light? ill supply code if it helps

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Narue
0

>while (!file.eof())
While this can be made to work, it requires unnecessary redundancy or an uncommon looping idiom. Anything else is incorrect because eof() only returns true after an attempt to read from the stream has failed, so the loop will iterate twice for the last record in the file. That's almost always a subtle and grievous error.

The best way to take input with a loop is to use the return value of your input function as the loop condition:

while ( getline ( file, string ) ) {
  // Stuff
}

Because getline returns an istream&, and istream has a conversion to void* for conditional tests, you can use the getline call as a loop condition and it will "just work".

>i need to read each string into a switch statement
You'll have to be more specific, strings can't be used as the condition for a switch statement. Cases must be integral.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.