Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   eof error using '>>' in fstream (http://www.velocityreviews.com/forums/t714386-eof-error-using-in-fstream.html)

David 02-09-2010 04:44 AM

eof error using '>>' in fstream
 
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::out);

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


David 02-09-2010 07:26 PM

Re: eof error using '>>' in fstream
 
On Feb 8, 10:24*pm, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
> David <david.l...@gmail.com> wrote in news:2341a9db-c380-4f33-ad65-
> db6d9e9c6...@k18g2000prf.googlegroups.com:
>
>
>
>
>
> > 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::out);

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



Kumar Anurag 02-11-2010 07:35 PM

Re: eof error using '>>' in fstream
 
On Feb 9, 11:24*am, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
> David <david.l...@gmail.com> wrote in news:2341a9db-c380-4f33-ad65-
> db6d9e9c6...@k18g2000prf.googlegroups.com:
>
>
>
>
>
> > 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::out);

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


??


All times are GMT. The time now is 01:37 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.