C ++ supports multi-language text characters and uses wide characters.
You only need to bind the input and output devices to a local locale object. Locale currentlocale ("chs"); indicates China
The processing method is exactly the same as that of the original single-byte character.
Various mappings are as follows:
Cin wcin
Cout wcout
Char wchar_t
String wstring
Ifstream wifstream
...
Before using any wide-byte stream object, do not forget to bind it to the local locale object, so that the stream object can support local characters.
The following program reads some sentences from the console and displays these sentence branches.
The single-byte program may be as follows:
[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main ()
{
String s;
Char c = '.';
While (getline (cin, s, c ))
{
Cout <s <endl;
}
}
# Include <iostream>
# Include <string>
Using namespace std;
Int main ()
{
String s;
Char c = '.';
While (getline (cin, s, c ))
{
Cout <s <endl;
}
}
The dubyte program may be like this:
[Cpp]
# Include <locale>
# Include <iostream>
# Include <string>
Using namespace std;
Int main ()
{
Locale china ("chs"); // use china character
Wcin. imbue (china); // use locale object
Wcout. imbue (china );
Wstring s;
Wchar_t wc = l '. '; // Wide character, wide string may be L "wide character"
While (getline (wcin, s, wc ))
{
Wcout <s <endl;
}
}
# Include <locale>
# Include <iostream>
# Include <string>
Using namespace std;
Int main ()
{
Locale china ("chs"); // use china character
Wcin. imbue (china); // use locale object
Wcout. imbue (china );
Wstring s;
Wchar_t wc = l '. '; // Wide character, wide string may be L "wide character"
While (getline (wcin, s, wc ))
{
Wcout <s <endl;
}
}
Getline is required to be obtained according to the delimiter. Therefore, do not forget to use the delimiter (Chinese periods)
The processing of Chinese text files may be as follows:
[Cpp]
Void writeToFile (wstring & s, string fileName)
{
Wofstream f;
F. imbue (locale ("chs "));
F. open (fileName. c_str (), ios: out );
F. seekp (0, ios: beg );
F <s;
F. close ();
F. clear ();
}
Void writeToFile (wstring & s, string fileName)
{
Wofstream f;
F. imbue (locale ("chs "));
F. open (fileName. c_str (), ios: out );
F. seekp (0, ios: beg );
F <s;
F. close ();
F. clear ();
}