Question

Using this code:

#include <fstream>

#include <boost/archive/text_oarchive.hpp>

using namespace std;

int main()
{
    std::ofstream ofs("c:\test");
    boost::archive::text_oarchive oa(ofs);
}

I'm getting an unhandled exception at runtime on executing the boost archive line:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::archive::archive_exception> >
Was it helpful?

Solution

The following line is in error:

 std::ofstream ofs("c:\test");

The compiler would've spit out a warning (at least) if your file was called jest; but '\t' -- being the escape for inserting a tab, your error goes by uncaught. In short, the file will not be created. You can test this with:

if (ofs.good()) { ... }

Now, since the file was not created, you don't have a valid iterator to pass on to boost::archive::text_oarchive which throws the exception.

Try this:

std::ofstream ofs("c:\\test");
//                  --^ (note the extra backslash)
if (ofs.good()) {
    boost::archive::text_oarchive oa(ofs);
    // ...
}

Hope this helps!

PS: A final nit I couldn't stop myself from making -- if you are going to use

using namespace std;

then

ofstream ofs("c:\\test");

is good enough. Of course, it is not an error to qualify ofstream, but it would not be the best coding style. But then, you know using using namespace is bad, don't you?

PPS:Thank you -- sharptooth for reminding me that \t gets you a tab!

OTHER TIPS

You need to catch the exception and then examine its exception_code to see what the root cause is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top