473,499 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with fstreams

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
9 2455
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
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3045
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
3729
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
2009
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
6
3200
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
117
7100
by: Peter Olcott | last post by:
www.halting-problem.com
28
5168
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
3784
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
4866
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
4529
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.