Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How Do We Set the L-value in.eof() to FALSE?

0 views
Skip to first unread message

Unknown

unread,
Aug 17, 2003, 10:07:01 AM8/17/03
to
It looks like there is a bug in a loop like

(while in.eof())
in.getline(line);
...
...
{}

when we work repeatedly on the same file.
We open the file "in" (ifstream), work on it and
reach its end of file. When we in.clear() and in.seekg(0) to the
file's beginning, we cannot use the above loop again. It looks like
the program has set the in.eof() bit and puts out an empty line each
time the second time over.
How do we set the L-value in.eof() to false?
Thanks!

Maria

Joseph M. Newcomer

unread,
Aug 17, 2003, 12:16:23 PM8/17/03
to
There may be a way. I avoid this entirely by never using the C++ I/O primitives. I would
open a CStdioFile and do GetString.

Note that eof() is not an L-value; what you are asking for is a method to clear the eof
state.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Unknown

unread,
Aug 17, 2003, 12:53:45 PM8/17/03
to
On Sun, 17 Aug 2003 12:16:23 -0400, Joseph M. Newcomer
<newc...@flounder.com> wrote:

>There may be a way. I avoid this entirely by never using the C++ I/O primitives. I would
>open a CStdioFile and do GetString.
>
>Note that eof() is not an L-value; what you are asking for is a method to clear the eof
>state.
> joe
>

Joe, you are right, but when I try something like "in.eof() = "
I get "l-value requried".
Thanks!

Maria

Rickey Braddam

unread,
Aug 17, 2003, 1:33:35 PM8/17/03
to
Hi, Maria. You probably know that in.eof() is a function. You can't assign a
value to a function. "in" probably has a variable called "EOF" that's
boolean, and you could try in.EOF = FALSE, but since there's an accessor
function, it's probably a private variable and you won't be able to set it.

By the way, intuitively, shouldn't a function in "in" named "in.eof()"
return TRUE when end of file is reached? If so, shouldn't your while loop be
"while (!in.eof())" instead?

Rickey

"maria >" <<> wrote in message
news:dlcvjv4cspraj4kgv...@4ax.com...

Rickey Braddam

unread,
Aug 17, 2003, 2:33:18 PM8/17/03
to
I think I found how to do what you want:

iostate state;
myfile.readstate(state);
state &= ~eof;
myfile.setstate( state );

But I think that the normal procedure would be to close then reopen the
file.

Sorry about the incomplete previous reply, hope this helps.

Rick

"Rickey Braddam" <shared...@earthlink.net> wrote in message
news:P%O%a.21217$BC2....@newsread2.news.atl.earthlink.net...

Lars-Inge Tønnessen

unread,
Aug 17, 2003, 4:22:07 PM8/17/03
to
> Joe, you are right, but when I try something like "in.eof() = "
> I get "l-value requried".

Are you sure your not a "=" short?
If you want to test anything it should be "==" (double ='s).

Lars-Inge


Lars-Inge Tønnessen

unread,
Aug 17, 2003, 4:39:23 PM8/17/03
to
This sample will read a text file called "filename.txt" 5 times using MFC.


CStdioFile file;
BOOL success = file.Open("filename.txt", CFile::modeRead |
CFile::typeText );
if ( success == TRUE )
{
for ( int counter = 0; counter < 5; counter++ )
{
CString read_text;
while ( file.ReadString( read_text ) == TRUE )
{
_tprintf("Read: (%s)\n", read_text );
}
_tprintf("__________________________________\n");
file.SeekToBegin(); // Start to read the file over again.
}
file.Close();
}
else
{
_tprintf("Could not find file!\n");
}


Lars-Inge


Doug Harrison [MVP]

unread,
Aug 17, 2003, 6:08:08 PM8/17/03
to
maria <<>> wrote:

>It looks like there is a bug in a loop like
>
>(while in.eof())
>in.getline(line);
>...
>...
>{}

In general, it's best to avoid looping around an EOF test, because it's
error-prone. I don't believe there's any guarantee that if an error occurs,
eofbit will be set, so you might end up in an infinite loop. Also, suppose
getline reads up to the very end of the file. Then eofbit won't be set until
the next read occurs, inside your loop, after you've tested for it. Your
loop body will continue with an empty string when it should have exited.
Here's some skeleton code, adapted from the C++ Standard, 27.6.1.3/22, which
shows a proper way to do this:

while (in.getline(buf, sizeof(buf)) || in.gcount())
{
// Read > 0 bytes
int count = in.gcount();
if (in.eof())
{
// OK, reached EOF while reading
}
else if (in.fail())
{
// An error occurred, but some data was read. Handle error.
}
else
{
// delimiter was extracted but not stored, so decrement count
--count;
}
// Process "count" bytes.
}

if (!in.eof())
{
// Handle error
}

>when we work repeatedly on the same file.
>We open the file "in" (ifstream), work on it and
>reach its end of file. When we in.clear() and in.seekg(0) to the
>file's beginning, we cannot use the above loop again. It looks like
>the program has set the in.eof() bit and puts out an empty line each
>time the second time over.
>How do we set the L-value in.eof() to false?

Calling in.clear() clears the eofbit, among other things, so you should be
fine there. How did you open your file? It should be easy to extract your
code into a very small console program which you can experiment with and
post here as source if you don't find the problem yourself.

--
Doug Harrison
Microsoft MVP - Visual C++

Unknown

unread,
Aug 17, 2003, 6:57:05 PM8/17/03
to
I have just discovered the following working piece of code as well.
It uses while(getline(in,string).

#include <string>
#include <fstream>
using namespace std;

int main() {
ifstream in("Scopy.cpp"); // Open for reading
ofstream out("Scopy2.cpp"); // Open for writing
string s;
while(getline(in, s)) // Discards newline char
out << s << "\n"; // ... must add it back
} ///:~

Thank you all guys for you help.

Maria

0 new messages