(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
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
>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
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...
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...
Are you sure your not a "=" short?
If you want to test anything it should be "==" (double ='s).
Lars-Inge
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
>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++
#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