eof error using '>>' in fstream

D

David

hello,
I'm trying to figure out a weird problem with opening a file for both
input and output using fstream...
compiling using g++ 4.3.2 on debian

-----
fstream f1;
f1.open("./test.txt", std::ifstream::in | std::ifstream::eek:ut);

f1 << "1 2 3\n" << flush; //dump something into file
f1.seekg(0); //set get ptr to 0

int i;
f1 >> i; //read out '1'

//cout << " get pos = " << f1.tellg() << " put pos = " << f1.tellp()
<< endl;
f1 << "a" << flush;

if (!f1.good())
{
cout << "fstream is bad" << endl;
}
else
{
cout << "fstream is good" << endl;
}

----
If i run the code above as is, I get eof error when trying to write
'a' into stream... text.txt reads "1 2 3"
if uncomment the cout, program completes and text.txt reads "1a2 3"

according to the '98 c++ open standard there seems to be some
inconsistencies on how seekg and seekp operates, but i can't find
anything that explains this behavior...
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#136

Any insights on why this is happening and how i can get this to work
without the cout is greatly appreciated!


Thanks!

~dAviD
 
D

David

(e-mail address removed):












The underlying system may require that you perform seek or some related
operation when switching between the reading and writing. This has little
to do with C++. For example, from the MSVC fopen() documentation:

"However, when you switch between reading and writing, there must be an
intervening fflush, fsetpos, fseek, or rewind operation."

Both tellg() and tellp() are documented as calling a seek (pubseekoff()),
so probably you just need to call one of them when switching from reading
to writing.

hth
Paavo

I see. I guess it didn't occur to me to look in fopen documentation
for clues.

from fopen manpage:
"Therefore it is good practice (and indeed sometimes necessary under
Linux) to put an fseek(3) or fgetpos(3) operation between write and
read operations on such a stream."

doesn't look like fflush call would work for linux, as it would for
MSVC.

Anyways, thanks much for your help!
 
K

Kumar Anurag

(e-mail address removed):












The underlying system may require that you perform seek or some related
operation when switching between the reading and writing. This has little
to do with C++. For example, from the MSVC fopen() documentation:

"However, when you switch between reading and writing, there must be an
intervening fflush, fsetpos, fseek, or rewind operation."

Both tellg() and tellp() are documented as calling a seek (pubseekoff()),
so probably you just need to call one of them when switching from reading
to writing.

hth
Paavo

??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top