Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > eof error using '>>' in fstream

Reply
Thread Tools

eof error using '>>' in fstream

 
 
David
Guest
Posts: n/a
 
      02-09-2010
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: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/wg...fects.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

 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      02-09-2010
On Feb 8, 10:24*pm, Paavo Helde <(E-Mail Removed)> wrote:
> David <(E-Mail Removed)> wrote in news:2341a9db-c380-4f33-ad65-
> (E-Mail Removed):
>
>
>
>
>
> > 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: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/wg...fects.html#136

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

>
> 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!


 
Reply With Quote
 
 
 
 
Kumar Anurag
Guest
Posts: n/a
 
      02-11-2010
On Feb 9, 11:24*am, Paavo Helde <(E-Mail Removed)> wrote:
> David <(E-Mail Removed)> wrote in news:2341a9db-c380-4f33-ad65-
> (E-Mail Removed):
>
>
>
>
>
> > 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: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/wg...fects.html#136

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

>
> 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


??
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ifstream eof not reporting eof? SpreadTooThin C++ 10 06-15-2007 08:49 AM
exception woes using fstream and .eof Greg C++ 7 12-19-2005 07:41 PM
if EOF = -1, can't a valid character == EOF and cause problems? Kobu C Programming 10 03-04-2005 10:40 PM
don't sync() on an fstream at EOF kartik C++ 1 10-30-2004 05:30 AM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM



Advertisments