Connecting Tech Pros Worldwide Help | Site Map

ofstream error conditions

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:59 AM
jois.de.vivre@gmail.com
Guest
 
Posts: n/a
Default 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


  #2  
Old July 23rd, 2005, 04:59 AM
Peter Julian
Guest
 
Posts: n/a
Default Re: ofstream error conditions


<jois.de.vivre@gmail.com> wrote in message
news:1117724579.869320.23460@z14g2000cwz.googlegro ups.com...[color=blue]
> 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
>[/color]

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.

  #3  
Old July 23rd, 2005, 05:00 AM
jois.de.vivre@gmail.com
Guest
 
Posts: n/a
Default Re: ofstream error conditions

Okay thanks, I discovered perror() works with C++ as well!

  #4  
Old July 23rd, 2005, 05:00 AM
Peter Julian
Guest
 
Posts: n/a
Default Re: ofstream error conditions


<jois.de.vivre@gmail.com> wrote in message
news:1117741686.117669.294160@g14g2000cwa.googlegr oups.com...[color=blue]
> Okay thanks, I discovered perror() works with C++ as well!
>[/color]

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.

*/

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.