Moving stream pointer to next line in file

Mar 8, 2012 at 11:17pm
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...
}
Mar 8, 2012 at 11:25pm
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 Mar 8, 2012 at 11:26pm
Mar 9, 2012 at 12:22am
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.
Mar 9, 2012 at 12:31am
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 Mar 9, 2012 at 12:32am
Mar 9, 2012 at 12:41am
I'm using the fopen_s() function in the CRT library. Is it better to handle files with the standard library?
Mar 9, 2012 at 12:45am
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.
Mar 9, 2012 at 1:40am
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 Mar 9, 2012 at 4:43am
Mar 9, 2012 at 12:47pm
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.