473,407 Members | 2,546 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,407 software developers and data experts.

ofstream file error checks?


Is there a way to catch file errors while writing
to a file using ofstream?
For ex., if the file is deleted or permissions changed
by another process after it is opened,
or when the disk is full.

I couldn't figure out which members could be used
to check for these types of errors.
I tried the good(), bad(), fail() etc.,
after writing to a full disk, deleted file etc.
They all returned success always.

Regarding the usage, I'm opening the file using
ofstream(....) and then using "<<" to do the writes.
Thanks for any help.
Jul 19 '05 #1
2 10530
steve <st****@w-steveu.ih.lucent.com> wrote in message
news:bh********@netnews.proxy.lucent.com...

Is there a way to catch file errors while writing
to a file using ofstream?
Yes. Depending upon which functions you use,
either check the function return value, or
check the stream state.
For ex., if the file is deleted or permissions changed
by another process after it is opened,
If your operating system allows one process to delete
or modify such attributes of a file that is already open
by another process, I think you need a new operating system. :-)
or when the disk is full.
If a write operation fails for whatever reason,
the stream state willbe in a 'fail' state. Check for

stream.good() == false
or
stream.fail() == true
or
stream.bad() == true

I couldn't figure out which members could be used
to check for these types of errors.
I tried the good(), bad(), fail() etc.,
after writing to a full disk, deleted file etc.
They all returned success always.
Show us the code.

Regarding the usage, I'm opening the file using
ofstream(....) and then using "<<" to do the writes.
Thanks for any help.


Show us the code. Try to compose a small compilable
program that exhibits the problem behavior.

-Mike

Jul 19 '05 #2
st****@w-steveu.ih.lucent.com (steve) writes:
Thanks for the response.
Below is the sample program I compiled with
CC on Unix(Solaris8) Sun Ultra. Executed in one shell
window. While on the sleep line, from another shell
window, ran "rm MyFile". When done, file was removed
but got all "SUCCESS" outputs on screen, never got
to the "FAIL" line, no coredump.
As I mentioned in my other post, this is correct Unix behaviour.
However, there *are* o/s specific means of locking a file - maybe you
should look into them (but you'll need a newsgroup like
comp.unix.programmer for that, since it goes beyond what C++
provides).
Tried similar scenario
where disk is full. Same result! Am I missing something?
Don't know about the disk full error - did you try flushing or closing
the file before assuming that the write succeeded?
================================================
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

main(int , char **)
{
string s1 = "MyFile";
ofstream out(s1.c_str(), ios::out|ios::app);
if(out.good()) {
cout << "good() - SUCCESS " << endl;
} else {
cout << "good() - FAIL " << endl;
}
out << "File opened" << endl;

sleep(10); // Another Unix shell window execute "rm MyFile"

out << "Hello World" << endl;
out.flush();

if(out.good()) {
cout << "good() - SUCCESS " << endl;
} else {
cout << "good() - FAIL " << endl;
}
if(out.fail()) {
cout << "fail() - FAIL " << endl;
} else {
cout << "fail() - SUCCESS " << endl;
}
if(out.bad()) {
cout << "bad() - FAIL " << endl;
} else {
cout << "bad() - SUCCESS " << endl;
}

out.close();


Try searching for some of James Kanze's articles in
comp.lang.c++.moderated, about why you shouldn't rely on calling
close() from within destructors (if the close fails, what do you do
then?). Maybe the close is failing here?

[snip]
--
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta
Jul 19 '05 #3

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

Similar topics

2
by: Steven C | last post by:
ofstream blah; IS: if (blah == NULL) { } THE SAME AS:
2
by: Armando | last post by:
i write one programm in vc++ but the compiler say that i use ambiguous symbol,i donīt know why ? how can do to tell the compiler that is ok so ? i become this error message: error C2872:...
3
by: Gurikar | last post by:
Hello, I have small clafification. #include<fstream> #include<iostream> using namespace std; void main() { ofstream ofs;
11
by: Gurikar | last post by:
Hello, ofstream ofs; fun(char* str) { ofs<<str<<endl; } void main()
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++...
2
by: thinktwice | last post by:
//case 1, works fine function(..) { std::ofstream out; out.open(file); out << "error info" <<endl; out.close(); } //case 2, declare it in a class
3
by: magix | last post by:
Hi, I got following to savefile into Output.txt, but how can I specify it to be in specific folder/directory (not at the same directory as the executables? Currently, it will generate the...
6
by: shyam | last post by:
Hi All I had raised a simillar query in an earlier post ...
15
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.