//使用MinGW在windows下面编译宽字符的例子
//g++ testwstring.cpp
//注意::源文件必须保存为UFT-8的格式
#include <iostream>
#include <locale>
#include <string>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
wstring ws=L"这是中国汉字";
wcout<<ws<<endl;
wcout<<L"字符个数::"<<ws.size()<<endl;
wcout<<L"<汉>所在的位置::"<<ws.find_first_of(L"汉")<<endl;
return 0;
}
//控制台下输出如下
//这是中国汉字
//字符个数::6
//<汉>所在的位置::4
//如果使用C++ Builder在控制台下编译宽字符,可以使用
//bcc32 testwstring.cpp
//在windows下对宽字符的支持,BCB显然比GCC要强.在C++ Builder 2010以后的版本直接支持UnicodeString
有的时候希望将宽字符(汉字)写入文件,请看如下示例:
//g++ testwstringfile.cpp
//将宽字符(汉字)写入文件
//注意::源文件必须保存为UFT-8的格式
#include <iostream>
#include <fstream>
#include <locale>
using namespace std;
int main(int argc, char** argv)
{
setlocale(LC_ALL,"chs");
wfilebuf wfb;
wostream output(&wfb);
wfb.open("WStringFile.txt", ios::in | ios::out |ios::trunc);
output << L"将汉字写入文件!" <<endl;
return 0;
}