Reputation: 43139
Seems like this should be simple, but I don't find it in a net search.
I have an ofstream which is open()
, and fail()
is now true. I'd like to know the reason for the failure to open, like with errno
I would do sys_errlist[errno]
.
Upvotes: 19
Views: 18539
Reputation: 97
Use:
GetLastError()
to get error code.FormatSystemMessage(GetLastError())
to get error message.Upvotes: 0
Reputation: 4325
This is portable but doesn't appear to give useful info:
#include <iostream>
using std::cout;
using std::endl;
#include <fstream>
using std::ofstream;
int main(int, char**)
{
ofstream fout;
try
{
fout.exceptions(ofstream::failbit | ofstream::badbit);
fout.open("read-only.txt");
fout.exceptions(std::ofstream::goodbit);
// successful open
}
catch(ofstream::failure const &ex)
{
// failed open
cout << ex.what() << endl; // displays "basic_ios::clear"
}
}
Upvotes: 2
Reputation: 525
we need not use std::fstream, we use boost::iostream
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
void main()
{
namespace io = boost::iostreams;
//step1. open a file, and check error.
int handle = fileno(stdin); //I'm lazy,so...
//step2. create stardard conformance streem
io::stream<io::file_descriptor_source> s( io::file_descriptor_source(handle) );
//step3. use good facilities as you will
char buff[32];
s.getline( buff, 32);
int i=0;
s >> i;
s.read(buff,32);
}
Upvotes: -4
Reputation: 35924
The strerror function from <cstring>
might be useful. This isn't necessarily standard or portable, but it works okay for me using GCC on an Ubuntu box:
#include <iostream>
using std::cout;
#include <fstream>
using std::ofstream;
#include <cstring>
using std::strerror;
#include <cerrno>
int main() {
ofstream fout("read-only.txt"); // file exists and is read-only
if( !fout ) {
cout << strerror(errno) << '\n'; // displays "Permission denied"
}
}
Upvotes: 19
Reputation:
Unfortunately, there is no standard way of finding out exactly why open() failed. Note that sys_errlist is not standard C++ (or Standard C, I believe).
Upvotes: 5