I am implementing serialization using Boost C++ libraries in a program that is built for Windows (using Visual Studio 2008) and Mac (using GCC). The program uses wide strings (std::wstring
) in about 30 of its classes. Depending on the platform, when I save to a file (by means of boost::archive::text_woarchive
), the wide strings are represented differently within the output file.
Saved under Windows:
H*e*l*l*o* *W*o*r*l*d*!* ...
Saved under MacOSX:
H***e***l***l***o*** ***W***o***r***l***d***!*** ...
where * is a NULL character.
When I try to read a file created under Windows using the Mac build (and vice versa), my program crashes.
From my understanding so far, Windows natively uses 2 bytes per wide character while MacOSX (and I suppose Unix in general) uses 4 bytes.
I have come across possible solutions such as utf8_codecvt_facet.cpp
, UTF8-CPP, ICU, and Dinkumware, but I have yet to see an example that will work with what I already have (e.g., I would prefer not re-writing five months of serialization work at this point):
std::wofstream ofs( "myOutputFile" );
boost::archive::text_woarchive oa( ... );
//... what do I put here? ...
oa << myMainClass;
myMainClass
contains wide strings and Boost smart pointers to other classes that, in turn, get serialized.
Boost. Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach. An overview of the features included in Boost. Asio, plus rationale and design information.
After 20 years of active Boost development, it's now recognized as a very powerful C++ library, for each major version many C++ libraries from the community were added. The Boost reviewers have an advanced C++ skills and their contributions guarantee a high quality for many years.
wofstream
is typedef basic_ofstream<wchar_t, char_traits<wchar_t> > wofstream;
on linux, you need to declare a custom ofstream
to deal with 16-bit characters (on linux).
This can be done as follows:
typedef std::uint16_t Char16_t;
typedef basic_ofstream<Char16_t, char_traits<Char16_t> > wofstream_16;
Now wofstream_16
can be used seamlessly on different platforms to deal with 16-bit wide chars.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With