473,386 Members | 1,644 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,386 software developers and data experts.

ofstream error conditions

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

Jul 23 '05 #1
3 9059

<jo***********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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


Its your responsability to check for errors and your choice what error you
actually check for. I gave my crystal ball away and my ESP levels are very
low. So without any code you are basicly on your own.

Jul 23 '05 #2
Okay thanks, I discovered perror() works with C++ as well!

Jul 23 '05 #3

<jo***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Okay thanks, I discovered perror() works with C++ as well!


In the case you prefer a C++ alternative:
Why not use try-catch blocks and std::exceptions to both generate error
messages and modify the execution path of the program?

// Filer.cpp

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdexcept>

int main()
{
std::string s_file("data.dat");
std::ofstream ofs;
std::ifstream ifs;
std::string s_buffer;
std::vector<std::string> vs;

try
{
// open file with output file stream,
// check for failure, otherwise write to it, close the ofs
ofs.open(s_file.c_str());
if (!ofs)
{
throw std::exception("(ofstream) error while opening file.");
}
for (int i = 0; i < 10; ++i)
{
ofs << "string " << i << std::endl;
}
ofs.close();

// open file with input file stream, read lines into vector
ifs.open(s_file.c_str());
if (!ifs)
{
throw std::exception("(ifstream) error while opening file.");
}

while (std::getline(ifs, s_buffer)) // getline until failure
{
vs.push_back(s_buffer);
}
if (!ifs.eof()) // if failure was not an eof marker, throw
{
throw std::exception("error while reading file.");
}

// display collected strings by iterating through vector
typedef std::vector<std::string>::iterator VITER;
for (VITER it = vs.begin(); it != vs.end(); ++it)
{
std::cout << *it << std::endl;
}

} // end of try block
catch (const std::exception& e)
{
std::cout << "Exception caught !!\n";
std::cout << e.what() << std::endl;
} // catch

return 0;
} // main

/*

string 0
string 1
string 2
string 3
string 4
string 5
string 6
string 7
string 8
string 9

if you write-protect data.dat, you'ld see the following message:

Exception caught !!
(ofstream) error while opening file.

*/

Jul 23 '05 #4

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

Similar topics

5
by: Ximo | last post by:
Hello, I'm programing an advanced calculator, and I have many problems with the execution errors, specually with the division by 0. And my question is how can show the execution error whitout...
5
by: DU | last post by:
Hi! I really need to understand what is so-called browser error correction mechanisms. Can you explain this? I read somewhere (and I no longer can find where I read that) that browsers try to...
9
by: Mark Twombley | last post by:
Hi, I'm just getting back into C++ and had a question about the best practice for assigning error numbers. I have been working in VB for sometime now and there you would start assigning error...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
6
by: Baloff | last post by:
Hello I wrote a code which is suppose to read a file which contains lines of double and prints it out. thanks for helping double.txt*************************************** 1.01 2.0301
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
5
by: grocery_stocker | last post by:
Why would someone go through the trouble of constructing an error handling function using variable-length argument lists? Why not just use something like printf()?
1
by: Taras_96 | last post by:
Hi everyone, I'm trying to use PHP's built in SoapClient class. It seems really easy to use, but am unsure of how to handle errors upon construction of the SoapClient object. If either the...
3
jhardman
by: jhardman | last post by:
I was in the middle of troubleshooting a different problem when my web service stopped working with a very cryptic error message: "specified cast is not valid" The only location info is that the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.