Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 28th, 2006, 12:05 AM
Mark P
Guest
 
Posts: n/a
Default getline questions

Consider the following snippet of code to read lines from a text file:

ifstream file_stream( "some_file.txt");
string read_line;
while( file_stream)
{
getline( file_stream, read_line);
}

I've tried this on two text files, one whose last line concluded with a
newline character (F1) and one whose last line did not conclude with a
newline character (F2). Then I look at the file_stream.eof() and (bool)
file_stream at various points in the reading process.

For F1 (final newline), I observe:
a. After reading the last line of text, eof() is false, and file_stream
is true.
b. After one more getline, eof() is true, file_stream is false, and
read_line is empty.

For F2 (no final newline), I observe:
a. After reading the last line of text, eof() is true, and file_stream
is true.
b. After one more getline, eof() is true, file_stream is false, and
read_line is the same as it was in the previous call (i.e., the last
line of text of the file).

So now I'm trying to make sense of all this and I have a couple questions:

1. Comparing F1.b. to F2.a., why is file_stream false in the first case
and true in the second case? We've hit the EOF in both cases-- is it
because in the latter we read some characters first?

2. Comparing F1.b. to F2.b., why is read_line empty in the first case
and unchanged from the previous value in the second case?

Thanks for your help,
Mark
  #2  
Old September 28th, 2006, 01:45 AM
Bob
Guest
 
Posts: n/a
Default Re: getline questions

On Wed, 27 Sep 2006 23:14:46 GMT, Mark P
<usenet@fall2005REMOVE.fastmailCAPS.fmwrote:
Quote:
>Consider the following snippet of code to read lines from a text file:
>
>ifstream file_stream( "some_file.txt");
>string read_line;
>while( file_stream)
>{
getline( file_stream, read_line);
>}
>
>I've tried this on two text files, one whose last line concluded with a
>newline character (F1) and one whose last line did not conclude with a
>newline character (F2). Then I look at the file_stream.eof() and (bool)
>file_stream at various points in the reading process.
>
>For F1 (final newline), I observe:
>a. After reading the last line of text, eof() is false, and file_stream
>is true.
>b. After one more getline, eof() is true, file_stream is false, and
>read_line is empty.
>
>For F2 (no final newline), I observe:
>a. After reading the last line of text, eof() is true, and file_stream
>is true.
>b. After one more getline, eof() is true, file_stream is false, and
>read_line is the same as it was in the previous call (i.e., the last
>line of text of the file).
>
>So now I'm trying to make sense of all this and I have a couple questions:
>
>1. Comparing F1.b. to F2.a., why is file_stream false in the first case
>and true in the second case? We've hit the EOF in both cases-- is it
>because in the latter we read some characters first?
If the function extracts no characters, it calls
is.setstate(ios_base::failbit) which may throw ios_base::failure.

In case F2.a, the read has resulted in extraction of characters. So
the file stream is still true or good.

In F1.b, no characters are extracted. The read has failed, so the
stream is set to fail or false.
Quote:
>2. Comparing F1.b. to F2.b., why is read_line empty in the first case
>and unchanged from the previous value in the second case?
In F1.b a sentry object is created because the file stream is still
true. Since the sentry object is true, it calls string::erase on the
passed string. So the string is erased.

In F2.a, the sentry object is false because the file stream is false.
So the passed string is not erased. It continues to contain the same
string as the previous call.

The sentry object performs some preliminary and cleanup work for the
stream.

Best wishes,

Bob
  #3  
Old September 28th, 2006, 06:25 AM
Klaus Füller
Guest
 
Posts: n/a
Default Re: getline questions

Mark P schrieb:
Quote:
Consider the following snippet of code to read lines from a text file:
>
ifstream file_stream( "some_file.txt");
string read_line;
while( file_stream)
{
getline( file_stream, read_line);
}
>
I've tried this on two text files, one whose last line concluded with a
newline character (F1) and one whose last line did not conclude with a
newline character (F2). Then I look at the file_stream.eof() and (bool)
file_stream at various points in the reading process.
>
For F1 (final newline), I observe:
a. After reading the last line of text, eof() is false, and file_stream
is true.
b. After one more getline, eof() is true, file_stream is false, and
read_line is empty.
>
For F2 (no final newline), I observe:
a. After reading the last line of text, eof() is true, and file_stream
is true.
b. After one more getline, eof() is true, file_stream is false, and
read_line is the same as it was in the previous call (i.e., the last
line of text of the file).
>
So now I'm trying to make sense of all this and I have a couple questions:
>
1. Comparing F1.b. to F2.a., why is file_stream false in the first case
and true in the second case? We've hit the EOF in both cases-- is it
because in the latter we read some characters first?
>
2. Comparing F1.b. to F2.b., why is read_line empty in the first case
and unchanged from the previous value in the second case?
Your "why?" has several aspects. There are technical reasons for the
behaviur you observed buried within the libraries. At the other hand
there are intended usages:

Always use 'file_stream' (e.g. the boolean value of the file-descriptor)
to check if data was retreived or if (otherwise) "something went wrong".
_If_ "something went wrong" (e.g. file_stream is false) the contents of
'read_line' has no real meaning: nothing has been read. To avoid
uninitialized variables the library returns an empty string in
'read_line' or what ever.

_After_ "something went wrong" (e.g. file_stream is false) you check for
EOF. As 'getline' returns the stream itsself the _idiom_ for the whole
loop is:

while( getline(file_stream, read_line) ) {
<Work on read_line>
}
if( !file_stream.eof() ) {
<Do further research on cause of error and ...>
<... possibly throw an exception>
}
// No 'else' if you threw an exception
<Go on>

You simply don't 'file_stream.eof()' before '(! file_stream)' and you
don't access 'read_line' after.

kf
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles