473,563 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10573
steve <st****@w-steveu.ih.lucen t.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.lucen t.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.progr ammer 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::a pp);
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++.m oderated, 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
2610
by: Steven C | last post by:
ofstream blah; IS: if (blah == NULL) { } THE SAME AS:
2
4047
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: 'ifstream' : ambiguous symbol error C2872: 'ofstream' : ambiguous symbol error C2065: 'fileread' : undeclared identifier error C2228: left of '.open' must...
3
3431
by: Gurikar | last post by:
Hello, I have small clafification. #include<fstream> #include<iostream> using namespace std; void main() { ofstream ofs;
11
3813
by: Gurikar | last post by:
Hello, ofstream ofs; fun(char* str) { ofs<<str<<endl; } void main()
3
9094
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++ functions that do this or is there any way I can check manually? Thanks, Prashant
2
5655
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
9925
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 output.txt under the same directory as the executables. std::stringstream stream; stream << "Output.txt" << std::flush; std::ofstream...
6
2044
by: shyam | last post by:
Hi All I had raised a simillar query in an earlier post http://groups.google.com/group/comp.lang.c++/browse_thread/thread/f371af248d90ead6/1d054627402539e1?lnk=gst&q=ofstream+write+failure&rnum=1#1d054627402539e1 Basically I have a file which is opened through ofstream for writing. Now I want to check if the file has been deleted or...
15
5981
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() .... std::ofstream fout; fout.open("hello.out"); fout<<setfill('-')<<setw(20)<<"-"<<setfill(' ')<<endl; fout<<"hello "; // this is line 139
0
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.