jeudi 28 avril 2016

In C++ std:: streams, after a failure, how to get a failure reason? Required: threadsafe and common to Windows and Linux (or at least Msvc/Gcc)

Sorry for weird title. Limited to 150 chars so couldn't use proper sentences.

So let's say I've done the following to find out that something went wrong with my file stream:

std::ofstream ofs;
do_stuff_with(ofs);
// streams don't throw on error because C++
// so we have to check for failure manually
if(!ofs){
    auto fail_code = errno; // threadsafe on Win/Linux
    // but what goes here?
}

1) strerror: Not threadsafe

2) strerror_s: Not in GCC? Or is it?

3) strerror_r: Not in Msvc? Or is it?

4) #ifdef/#define/etc: yuck, but may be the only choice

I did do some searching but I didn't find a "this will definitely work in a sensible yet slightly platform-dependent way" answer...

Aucun commentaire:

Enregistrer un commentaire