By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Problem with fstreams

P: n/a
Hello NG.

I'm trying to read in a textfile. I will give you an extract of my code:

bool LogfileHandle::ReadFile( string filename )
{
std::ifstream LogFile( filename.c_str() );
if( LogFile.is_open() )
{
std::string Line;
int Pos = 0;
while( !LogFile.eof() )
{
Pos = LogFile.tellg();
std::getline( LogFile, Line );

// test, if line is a message
if( Line.find( "Logging started" ) == string::npos )
{
// here some information is extracted from Line
}
Line.clear();
}
LogFile.close();
return true;
}
else
{
return false;
}
}

and these are the first 2 lines of the file i try to read in:

Logging started:10-2-2006 12:45:36
Logging started:10-2-2006 12:53:32

Now the problem that occurs is, that getline() seems to mess up the
stream pointers. Running the while-loop for the first time,
LogFile.tellg() returns 0. getline then saves the first line of the file
into Line as expected. The second time the while loop runs
LogFile.tellg() returns 42, which is not at the beginning of the second
line in the file as expected, but somewhere in the middle.
Maybe one of you can figure out what goes wrong.

Thanks in advance.

--
Mit freundlichen Grüßen<br>
<br>
Thomas Helmke
May 17 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Thomas Helmke wrote:
Hello NG.

I'm trying to read in a textfile. I will give you an extract of my code:

bool LogfileHandle::ReadFile( string filename )
{
std::ifstream LogFile( filename.c_str() );
if( LogFile.is_open() )
{
std::string Line;
int Pos = 0;
while( !LogFile.eof() )
This loop is incorrect. If some error occurs while reading the file, you get
an endless loop. If not, you get a loop that runs once too often, because
the eof bit is set _after_ you tried to read past the end of the file.
{
Pos = LogFile.tellg();
std::getline( LogFile, Line );

// test, if line is a message
if( Line.find( "Logging started" ) == string::npos )
{
// here some information is extracted from Line
}
Line.clear();
}
LogFile.close();
return true;
}
else
{
return false;
}
}

and these are the first 2 lines of the file i try to read in:

Logging started:10-2-2006 12:45:36
Logging started:10-2-2006 12:53:32

Now the problem that occurs is, that getline() seems to mess up the
stream pointers. Running the while-loop for the first time,
LogFile.tellg() returns 0. getline then saves the first line of the file
into Line as expected. The second time the while loop runs
LogFile.tellg() returns 42, which is not at the beginning of the second
line in the file as expected, but somewhere in the middle.
Maybe one of you can figure out what goes wrong.


Do you mean that reading the second line doesn't work as expected?

May 17 '06 #2

P: n/a
Rolf Magnus schrieb:
Thomas Helmke wrote:
Hello NG.

I'm trying to read in a textfile. I will give you an extract of my code:

bool LogfileHandle::ReadFile( string filename )
{
std::ifstream LogFile( filename.c_str() );
if( LogFile.is_open() )
{
std::string Line;
int Pos = 0;
while( !LogFile.eof() )


This loop is incorrect. If some error occurs while reading the file, you get
an endless loop. If not, you get a loop that runs once too often, because
the eof bit is set _after_ you tried to read past the end of the file.


Ok. So I have to check for eof() after the getline() and then break the
loop?
{
Pos = LogFile.tellg();
std::getline( LogFile, Line );

// test, if line is a message
if( Line.find( "Logging started" ) == string::npos )
{
// here some information is extracted from Line
}
Line.clear();
}
LogFile.close();
return true;
}
else
{
return false;
}
}

and these are the first 2 lines of the file i try to read in:

Logging started:10-2-2006 12:45:36
Logging started:10-2-2006 12:53:32

Now the problem that occurs is, that getline() seems to mess up the
stream pointers. Running the while-loop for the first time,
LogFile.tellg() returns 0. getline then saves the first line of the file
into Line as expected. The second time the while loop runs
LogFile.tellg() returns 42, which is not at the beginning of the second
line in the file as expected, but somewhere in the middle.
Maybe one of you can figure out what goes wrong.


Do you mean that reading the second line doesn't work as expected?


Yes. The first getline() puts "Logging started:10-2-2006 12:45:36" into
the Line string. The second getline() puts "g started:10-2-2006
12:53:32" into the Line string which corresponds to the position
indicated by the second tellg().
May 17 '06 #3

P: n/a
Thomas Helmke wrote:
Rolf Magnus schrieb:
Thomas Helmke wrote:
Hello NG.

I'm trying to read in a textfile. I will give you an extract of my code:

bool LogfileHandle::ReadFile( string filename )
{
std::ifstream LogFile( filename.c_str() );
if( LogFile.is_open() )
{
std::string Line;
int Pos = 0;
while( !LogFile.eof() )


This loop is incorrect. If some error occurs while reading the file, you
get an endless loop. If not, you get a loop that runs once too often,
because the eof bit is set _after_ you tried to read past the end of the
file.


Ok. So I have to check for eof() after the getline() and then break the
loop?


You shoulnd't check for eof specifically. As I said, if you get a read
error, the file will never reach eof and thus you are stuck in the loop
forever.
A common idiom is:

while (std::getline(LogFile, Line))
{
//...
}

See also question 15.2, 15.4 and 15.5 in the FAQ to this newsgroup. They are
about operator>>, but std::getline follows the same principle.
{
Pos = LogFile.tellg();
std::getline( LogFile, Line );

// test, if line is a message
if( Line.find( "Logging started" ) == string::npos )
{
// here some information is extracted from Line
}
Line.clear();
}
LogFile.close();
return true;
}
else
{
return false;
}
}

and these are the first 2 lines of the file i try to read in:

Logging started:10-2-2006 12:45:36
Logging started:10-2-2006 12:53:32

Now the problem that occurs is, that getline() seems to mess up the
stream pointers. Running the while-loop for the first time,
LogFile.tellg() returns 0. getline then saves the first line of the file
into Line as expected. The second time the while loop runs
LogFile.tellg() returns 42, which is not at the beginning of the second
line in the file as expected, but somewhere in the middle.
Maybe one of you can figure out what goes wrong.


Do you mean that reading the second line doesn't work as expected?


Yes. The first getline() puts "Logging started:10-2-2006 12:45:36" into
the Line string. The second getline() puts "g started:10-2-2006
12:53:32" into the Line string which corresponds to the position
indicated by the second tellg().


Hmm, I don't see any error that could explain that in the code you gave. Is
it really the code you tried?
May 17 '06 #4

P: n/a
Rolf Magnus schrieb:
Thomas Helmke wrote:
Rolf Magnus schrieb:
Thomas Helmke wrote:

Hello NG.

I'm trying to read in a textfile. I will give you an extract of my code:

bool LogfileHandle::ReadFile( string filename )
{
std::ifstream LogFile( filename.c_str() );
if( LogFile.is_open() )
{
std::string Line;
int Pos = 0;
while( !LogFile.eof() )
This loop is incorrect. If some error occurs while reading the file, you
get an endless loop. If not, you get a loop that runs once too often,
because the eof bit is set _after_ you tried to read past the end of the
file.

Ok. So I have to check for eof() after the getline() and then break the
loop?


You shoulnd't check for eof specifically. As I said, if you get a read
error, the file will never reach eof and thus you are stuck in the loop
forever.
A common idiom is:

while (std::getline(LogFile, Line))
{
//...
}

See also question 15.2, 15.4 and 15.5 in the FAQ to this newsgroup. They are
about operator>>, but std::getline follows the same principle.
{
Pos = LogFile.tellg();
std::getline( LogFile, Line );

// test, if line is a message
if( Line.find( "Logging started" ) == string::npos )
{
// here some information is extracted from Line
}
Line.clear();
}
LogFile.close();
return true;
}
else
{
return false;
}
}

and these are the first 2 lines of the file i try to read in:

Logging started:10-2-2006 12:45:36
Logging started:10-2-2006 12:53:32

Now the problem that occurs is, that getline() seems to mess up the
stream pointers. Running the while-loop for the first time,
LogFile.tellg() returns 0. getline then saves the first line of the file
into Line as expected. The second time the while loop runs
LogFile.tellg() returns 42, which is not at the beginning of the second
line in the file as expected, but somewhere in the middle.
Maybe one of you can figure out what goes wrong.
Do you mean that reading the second line doesn't work as expected?

Yes. The first getline() puts "Logging started:10-2-2006 12:45:36" into
the Line string. The second getline() puts "g started:10-2-2006
12:53:32" into the Line string which corresponds to the position
indicated by the second tellg().


Hmm, I don't see any error that could explain that in the code you gave. Is
it really the code you tried?


Yes. Even when I perform a tellg() directly after getline() the position
is not the first character of the second line but some 5 or 6 characters
ahead. Very strange indeed.
May 17 '06 #5

P: n/a
JE

Thomas Helmke wrote:
Rolf Magnus schrieb:
Thomas Helmke wrote: <snip> The first getline() puts "Logging started:10-2-2006 12:45:36"
into
the Line string. The second getline() puts "g started:10-2-2006
12:53:32" into the Line string which corresponds to the position
indicated by the second tellg().

Rolf > > Hmm, I don't see any error that could explain that in the code
you gave. Is
Rolf > > it really the code you tried?

Yes. Even when I perform a tellg() directly after getline() the position
is not the first character of the second line but some 5 or 6 characters
ahead. Very strange indeed.


Are you sure your lines are terminated with '\n' and not '\0'? I'm not
sure how
'n' would get interpreted as '\n', though...

JE

May 17 '06 #6

P: n/a
JE schrieb:
Thomas Helmke wrote:
Rolf Magnus schrieb:
Thomas Helmke wrote: <snip> The first getline() puts "Logging started:10-2-2006 12:45:36"
into the Line string. The second getline() puts "g started:10-2-2006
12:53:32" into the Line string which corresponds to the position
indicated by the second tellg().

Rolf > > Hmm, I don't see any error that could explain that in the code
you gave. Is
Rolf > > it really the code you tried?
Yes. Even when I perform a tellg() directly after getline() the position
is not the first character of the second line but some 5 or 6 characters
ahead. Very strange indeed.


Are you sure your lines are terminated with '\n' and not '\0'? I'm not
sure how
'n' would get interpreted as '\n', though...

JE


The lines are terminated with CR/LF.
I now fixed the problem with a workaround:

if( Pos != Size )
{
while( LogFile.peek() != '\n' )
{
Pos--;
LogFile.seekg( Pos );
}
Pos++;
LogFile.seekg( Pos );
}

where Size is the filesize. This works for the moment.

thanks for your help!
May 18 '06 #7

P: n/a
JE

Thomas Helmke wrote:
JE schrieb:
Thomas Helmke wrote:
Rolf Magnus schrieb:
Thomas Helmke wrote:

<snip> The first getline() puts "Logging started:10-2-2006 12:45:36"
into
> the Line string. The second getline() puts "g started:10-2-2006
> 12:53:32" into the Line string which corresponds to the position
> indicated by the second tellg().

Rolf > > Hmm, I don't see any error that could explain that in the code
you gave. Is
Rolf > > it really the code you tried?
Yes. Even when I perform a tellg() directly after getline() the position
is not the first character of the second line but some 5 or 6 characters
ahead. Very strange indeed.


Are you sure your lines are terminated with '\n' and not '\0'? I'm not
sure how
'n' would get interpreted as '\n', though...

JE


The lines are terminated with CR/LF.
I now fixed the problem with a workaround:

if( Pos != Size )
{
while( LogFile.peek() != '\n' )
{
Pos--;
LogFile.seekg( Pos );
}
Pos++;
LogFile.seekg( Pos );
}

where Size is the filesize. This works for the moment.

thanks for your help!


You might want to check for undefined behavior or other problems (for
example, if something is using your log file _while_ you are
processing).

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s("Logging started:10-2-2006 12:45:36\r\n"
"Logging started:10-2-2006 12:53:32\r\n");
std::istringstream LogFile(s);
std::string Line;
while(std::getline(LogFile, Line))
{
if(Line.find("Logging started") == std::string::npos)
{
std::cerr << "A miracle occurred\n";
}
else
{
std::cerr << "Processing: " << Line << '\n';
}

Line.clear();
}
}

// Output:
// Processing: Logging started:10-2-2006 12:45:36
// Processing: Logging started:10-2-2006 12:53:32

May 19 '06 #8

P: n/a
HI Thomas,

I am Sajjad and I am also facing the similar problem could you please
explain me what exactly is going on behind the code.

Thanks,
Sajjad

May 22 '06 #9

P: n/a
"Skilled" writes:
HI Thomas,

I am Sajjad and I am also facing the similar problem could you please
explain me what exactly is going on behind the code.


If you want to send mail to Thomas, send it to *him*. You accidentally
posted it to a newsgroup, where it has absolutely no meaning whatsoever,
since there is no context..
May 22 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.