file access problem

F

Flykiller

I have the following code:

string line1 ;
ifstream fin ;
ofstream fout ;

fin.open("inputfile.txt") ;
fout.open("outputfile.txt") ;

while (getline(fin,line1))
fout << line1 << etc ;

fin.close() ;
fout.close() ;

fin.open("outputfile.txt") ;

at this point outputfile.txt exists, and has data I expect and want, but fin
opens at EOF. I can't read anything out of outputfile.txt. I've tried
fin.open('outputfile.txt", ios::in), same result. I've never seen this before,
and neither has anyone I've been able to talk to. this is on linux using g++.
if someone can point out what I'm doing wrong, or how to work around this, I'd
appreciate it.

if it's relevant, the machine I'm using also has the following problem. it
compiles this:

int main()
{
using namespace std ;
}

but does not compile this:

using namespace std ;
int main()
{
}
 
S

Sharad Kala

Flykiller said:
I have the following code:

string line1 ;
ifstream fin ;
ofstream fout ;

fin.open("inputfile.txt") ;
fout.open("outputfile.txt") ;

while (getline(fin,line1))
fout << line1 << etc ;

fin.close() ;
fout.close() ;

The reason is that the stream has gone into bad state. Neither close nor
open re-instate the good bit. So call clear() on the input stream and it
should be fine.
fin.open("outputfile.txt") ;

-Sharad
 
F

Flykiller

The reason is that the stream has gone into bad state. Neither close nor
open re-instate the good bit. So call clear() on the input stream and it
should be fine.

it worked. but rather than just slap that in and move on, can I find out how
the stream went into a bad state in the first place? or is it standard
practice to follow close() with clear() and I simply didn't know that?
 
J

John Harrison

it worked. but rather than just slap that in and move on, can I find
out how
the stream went into a bad state in the first place? or is it standard
practice to follow close() with clear() and I simply didn't know that?

Your stream went into a bad state because you read to the end of file.

john
 
F

Flykiller

Your stream went into a bad state because you read to the end of file.

I'm hardly a guru but I've worked with files before and I've never had to
specifically clear() a stream like this. close() and open() always seemed to
be enough. anyway, thanks all.
 
J

John Harrison

I'm hardly a guru but I've worked with files before and I've never had to
specifically clear() a stream like this. close() and open() always
seemed to
be enough. anyway, thanks all.

Some implementations do not implement the C++ standard correctly.

This issue is actually being reviewed by the standards committee and the
standard may change to something more sensible in the future.

john
 

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
474,174
Messages
2,570,941
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top