ofstream and close()

F

Fraser Ross

If the failbit becomes set after calling close() what issues are there?
Will subsequent calls to open() fail even after calling clear()? If the
buffer was flushed will the file be ok despite close() setting the
failbit?

Fraser.


*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
B

BobR

Fraser Ross wrote in message
If the failbit becomes set after calling close() what issues are there?
Will subsequent calls to open() fail even after calling clear()? If the
buffer was flushed will the file be ok despite close() setting the
failbit?
Fraser.

Try this, then post again.

#include <iostream> // C++
#include <ostream> // std::endl
#include <fstream>
#include <string>
#include <vector>
// ------------------------------------
void TestFileBinaryPing( std::eek:stream &cout ){
std::string File1( "putaname1.type" ); <-- fill these in
std::string File2( "putaname2.type" );

std::ifstream Ping(File1.c_str(), std::ios_base::binary );
if(!Ping){
cout<<"\n ifstream 1 FAILED"<<std::endl;
}
cout<<" ifstream Ping.tellg() = "<<Ping.tellg()<<std::endl;
Ping.seekg(0, std::ios::end);
cout<<" Ping.seekg(0, ios::end) Ping.tellg() =
"<<Ping.tellg()<<std::endl;
Ping.seekg(0, std::ios::beg);
cout<<" Ping.seekg(0, ios::beg) Ping.tellg() =
"<<Ping.tellg()<<std::endl;
std::vector<unsigned char> Image;
char In(0);
while( Ping.get(In) ){
Image.push_back( static_cast<unsigned char>( In ) );
}
cout<<"\n Image.size() = "<<Image.size()<<" bytes."<<std::endl;
cout<<" ifstream Ping.tellg() = "<<Ping.tellg()<<std::endl;
Ping.close();
// note: use the SAME stream
Ping.open(File2.c_str(), std::ios_base::binary );
if(!Ping){
cout<<"\n ifstream 2 FAILED"<<std::endl;
}
cout<<" Ping.good() = "<<Ping.good()<<std::endl;
Ping.seekg(0, std::ios::end);
cout<<" Ping.seekg(0, ios::end) Ping.tellg() =
"<<Ping.tellg()<<std::endl;
// note: now clear stream, and note how it works
Ping.clear();
Ping.seekg(0, std::ios::end);
cout<<" Ping.clear(); Ping.seekg(0, ios::end) Ping.tellg() = "
<<Ping.tellg()<<std::endl;
cout<<" Ping.good() = "<<Ping.good()<<std::endl;
Ping.seekg(0, std::ios::beg);
cout<<" Ping.seekg(0, ios::beg) Ping.tellg() = "
<<Ping.tellg()<<std::endl;
Ping.get(In);
cout<<std::endl;
// ------------------------------------
return;
}
// ------------------------------------

int main(){
TestFileBinaryPing( std::cout );
return 0;
}
// ------------------------------------

[Please excuse the 'naming', I copied out of a testing program.]
 
F

Fraser Ross

"BobR"
Fraser Ross wrote in message


Try this, then post again.

The program isn't demonstrating anything I want to know about. What is
there that can make close() fail other than flushing the buffer to disk?
If that is already done then there must be no reason for close() setting
the failbit.

Fraser.


*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
B

BobR

Fraser Ross wrote in message
The program isn't demonstrating anything I want to know about.

Then why did you ask:
My code demonstrated that even in a failed state(due to EOF/'close()') the
stream could open a second file, you just could not use it until you
'clear()' it.
What is
there that can make close() fail other than flushing the buffer to disk?

When you do:

MyFileStream << "hello" << std::endl;
MyFileStream.close();

....the 'endl' flushes the stream. But, what if 'MyFileStream' was opened on a
floppy disk and just before that line you pulled the disk out of the drive?
The stream would go into a fail state, right? 'close()' could not flush the
buffer. There's one.
When you read a file until EOF, the stream is in a failed state, the
'close()' is probably ignored. You need to 'clear()' the stream to do
anything to it (like 'seekg()').
If that is already done then there must be no reason for close() setting
the failbit.
Fraser.

The 'close()' is seldom used, we usually just let the stream go out of scope
and destruct. I would assume that during the destruct, 'close()' would be
issued. Setting the failbit in 'close()' is logical, it tells you that the
stream is not usable in it's current state. You must find why it failed, fix
the problem and reset it before you can use the stream further. Did you try
my code?

Do you have an specific task in mind?

[ corrections are welcome ]
 
F

Fraser Ross

What is
disk?

When you do:

MyFileStream << "hello" << std::endl;
MyFileStream.close();

...the 'endl' flushes the stream. But, what if 'MyFileStream' was opened on a
floppy disk and just before that line you pulled the disk out of the drive?
The stream would go into a fail state, right? 'close()' could not flush the
buffer. There's one.

I was thinking about the case where the stream has a good state and is
already flushed when close() is called. I can't see any reason for
close() setting the failbit there.

I prefer to close a stream and clear() it when I'm done with it rather
than leave these jobs to the code that reopens the stream.

Fraser.


*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top