fstream处理中文时的小问题

VS2005   C++  Unicode

 

用fstream输出和读入有中文内容的文本文件时有问题

 

在缺省的C locale下,ofstream能正确输出中文到文件中,但不支持中文文件名;wofstream支持中文文件名,但不能向文件中输出中文。要解决这个问题,需要在打开文件之前将全局locale设为本地语言。将全局locale设为本地语言后,ofstream和wofstream的问题都解决了,但 cout和wcout却不能输出中文了。要让cout和wcout输出中文,需要将全局locale恢复原来的设置

 

   locale &loc=locale::global(locale(locale(),"",LC_CTYPE));   ofstream ofs("ofs测试.txt");   wofstream wofs(L"wofs测试.txt");   locale::global(loc);   ofs<<"test测试"<<1234<<endl;   wofs<<L"Another test还是测试"<<1234<<endl;

 

相关链接:http://blog.csdn.net/iiprogram/archive/2008/10/13/3067720.aspx

你可能感兴趣的:(c,测试,语言)