Moving stream pointer to next line in file

Is there a way to move the position without reading the line? I want to just read the first char on the line, and if it is not the one i want it moves onto the next line.
1
2
3
4
5
6
7
8
if ( (char)fgetc( file ) == ';' )
{
//move to next line
}
else
{
//other code...
}
MyCPlusPlusStdStreamObject.ignore(unsigned(-1), '\n');

By the way, comments are a bit more complex than just that. What if spaces preceed it? ;)
Last edited on
Can you give me a link to msdn? I can't find that function anywhere. Also, in this file format there can only be comments at the beginning anyway.
MSDN link? This is a member function on the std::ifstream object, which is the usual class people use to read from files in C++: http://www.cplusplus.com/reference/iostream/ifstream/

If you want to do it using Windows-Only code, you will just have to keep reading in bytes until you read \n - which is a bit more than just one line of code.
Last edited on
I'm using the fopen_s() function in the CRT library. Is it better to handle files with the standard library?
Yes, it is much easier, and the code will work on any platform. Even if you're staying on Windows, the ease of the C++ library is more than worth it.
I just have one more question. When I call ifstream->close() after using the ignore(), does that remove the line from the file or does it just ignore it when it is reading it?

EDIT: Nevermind, it doesn't.
Last edited on
ifstream can only do input operations, meaning it can work with files marked as read only. To output to files use ofstream. ;)
Topic archived. No new replies allowed.