C ++ reads and writes Chinese characters, and C ++ processes Chinese Characters

Source: Internet
Author: User

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 ();
}
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.