473,378 Members | 1,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

ofstream and close()

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 ***
Mar 29 '06 #1
4 4266

Fraser Ross wrote in message
<44***********************@titian.nntpserver.com>. ..
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::ostream &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.]
--
Bob R
POVrookie
Mar 29 '06 #2
"BobR"

Fraser Ross wrote in message
<44***********************@titian.nntpserver.com>. ..
If the failbit becomes set after calling close() what issues are there?Will subsequent calls to open() fail even after calling clear()? If thebuffer was flushed will the file be ok despite close() setting the
failbit?
Fraser.


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 ***
Mar 30 '06 #3

Fraser Ross wrote in message
<44***********************@titian.nntpserver.com>. ..

The program isn't demonstrating anything I want to know about.
Then why did you ask:
>Will subsequent calls to open() fail even after calling clear()?


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 ]
--
Bob R
POVrookie
Mar 30 '06 #4
> > 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.


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 ***
Mar 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
3
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++...
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
0
by: fasteddie203 | last post by:
Hi, I hope this is appropriate for standard C++ group, I apologize if it is not. I'm trying to print to LPT1 using ofstream which works fine as long as the printer is connected - it doesn't need...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
3
by: Howard Kaikow | last post by:
I was having difficulty trying to use ofstream in C++ 2003, so I booted to another system that has VS C++ 6 and got the code working. I then import the project into VS C++ .NET 2002. Code ran as...
5
by: wobudui | last post by:
Hi everyboday, I have some trouble in dealing with the file stream. My souce code Listed hear: int main() { char buffer={0}; ofstream ofile.open("mydata.in",ios::app); ofile.seekp(10);...
2
by: bob | last post by:
Hi, I'm working with an ofstream object and using it to print the contents on an 100 * 100 matrix. i.e. lots of elements. the _matrix variable below is defined as std::vector<std::string,...
5
by: Joe Hesse | last post by:
Hi, I have a C++ function that writes to an ofstream object. I would like to sometimes use it to write to cout. I realize that cout is of type ostream which is not ofstream. Since cout is "kind...
1
MrPickle
by: MrPickle | last post by:
I am tokenizing a string and sending it to a ofstream but I am getting strange results. The \n sequence isn't working; it doesn't go to a new line. I'm getting Chinese/Japanese characters in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.