I have this code to serialize/deserialize class objects to file, and it seems to work. However, I have two questions.
- What if instead two
wstring
's (as I have now) I want to have onewstring
and onestring
member variable in my class? (I think in such case my code won't work?). - Finally, below, in main, when I initialize
s2.product_name_= L"megatex";
if instead of megatex I write something in Russian say (e.g., s2.product_name_= L"логин"), the code doesn't work anymore as intended.
What can be wrong? Thanks.
Here is code:
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream> // std::ifstream
using namespace std;
// product
struct Product
{
double price_;
double product_index_;
wstring product_name_;
wstring other_data_;
friend std::wostream& operator<<(std::wostream& os, const Product& p)
{
return os << p.price_ << endl
<< p.product_index_ << endl
<< p.product_name_ << endl
<< p.other_data_ << endl;
}
friend wistream& operator>>(std::wistream& is, Product& p)
{
is >> p.price_ >> p.product_index_;
is.ignore(std::numeric_limits<streamsize>::max(), '\n');
getline(is,p.product_name_);
getline(is,p.other_data_);
return is;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Product s1,s2;
s1.price_ = 100;
s1.product_index_ = 0;
s1.product_name_= L"flex";
s1.other_data_ = L"dat001";
s2.price_ = 300;
s2.product_index_ = 2;
s2.product_name_= L"megatex";
s2.other_data_ = L"dat003";
// write
wofstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app);
binary_file << s1 << s2;
binary_file.close();
// read
wifstream binary_file2("c:\\test.dat");
Product p;
while (binary_file2 >> p)
{
if(2 == p.product_index_){
cout<<p.price_<<endl;
cout<<p.product_index_<<endl;
wcout<<p.product_name_<<endl;
wcout<<p.other_data_<<endl;
}
}
if (!binary_file2.eof())
std::cerr << "error during parsing of input file\n";
else
std::cerr << "Ok \n";
return 0;
}
There are an inserter defined for
char *
for anybasic_ostream
(ostream
andwostream
), so you can use the result ofc_str()
member function call for thestring
member. For example, if thestring
member isother_data_
:The extractor case is more complex, since you'll have to read as
wstring
and the convert tostring
. The most simple way to do this is just reading aswstring
and then narrowing each character:I'm not using locales in this sample, just converting a sequence of bytes (8 bits) to a sequence of words (16 bits) for output and the opposite (truncating values) for input. That is OK if you are using ASCII chars, or using single-byte chars and you don't require an specific format (as Unicode) for output.
Otherwise, you will need handle with
locale
s. locale gives cultural contextual information to interpret the string (remember that is just a sequence of bytes, not characters in the sense of letters or symbols; the map between the bytes and what symbol represents is defined by thelocale
).locale
is not an very easy to use concept (human culture isn't too). As you suggest yourself, it would be better make first some investigation about how it works.Anyway, the idea is:
string
s from original charset to Unicode usinglocale
for output.wstring
s read from file (in Unicode) tostring
s usinglocale
.When you define an array of
wchar_t
usingL""
, you'are not really specifying the string is Unicode, just that the array is of chars, not wchar_t. I suppose the intended working iss2.product_name_
store the name in Unicode format, but the compiler will take everychar
in that string (as withoutL
) and convert towchar_t
just padding with zeros the most significant byte. Unicode is not good supported in the C++ standard until C++11 (and is still not really too supported). It works just for ASCII characters because they have the same codification in Unicode (or UTF-8).For using the Unicode characters in a static string, you can use escape characters:
\uXXXX
. Doing that for every not-English character is not very comfortable, I know. You can found a list of Unicode characters in multiple sites in the web. For example, in the Wikipedia: http://en.wikipedia.org/wiki/List_of_Unicode_characters.