2024年3月6日发(作者:)

用ifstream 和ofstream 读写中文的总结:

首先读写中文应为wifstream 和 ofstream。

下面的我也不知道为什么?但是可以正常使用、

#include

#include

#include

using namespace std;

int main()

{

setlocale(LC_ALL,"chs");

//设置wcout,能正常显示中文

wchar_t ch ,str[]={ L"一二三四五六七八"};

wofstream outfile("");

(std::locale("chs")); //可以正常写入

if (!outfile)

{

cout << "不能建立文件!"<

exit(1);

}

(str,wcslen(str));

();

();

locale loc("chs");

wifstream infile;

(loc);//可以正常读出

("");

if (!infile)

{

cout << "不能打开文件!"<

exit(1);

}

(0,ios::beg);

(&ch,1);

wcout << L"第一个字符是:" << ch <

();

}

另外还有一些东西:

char ch2;

wchar_t ch;

cout << sizeof(ch2) <

cout << sizeof(ch)<

cout << sizeof(L'中')<

cout << sizeof(L"中")<

cout << sizeof('中')<

cout << sizeof("中国人")<

cout << wcslen(L"中") <

有意思!