#include<iostream>
#include<fstream>
#include<string>
#include <windows.h>
int main()
{
//locale china("zh_CN.UTF-8");
//locale::global(locale::global(locale(locale(), "", LC_CTYPE)));
wcout.imbue(locale("", LC_CTYPE));//将读码格式设置为本地格式
wchar_t a;
wstring ss;
int count[62] = { 0 };
wifstream read(L"D:/工作/学习/编程/Annabel Lee.txt", ios::in);
wofstream write(L"D:/工作/学习/编程/Chinese Annabel Lee.txt", ios::trunc);
if (!read.is_open()) wcout << L"文件读取失败。" << endl;
if (!write.is_open()) wcout << L"文件输出失败。" << endl;
write << "不是wofstream有问题,是我有问题。" << endl;
//while (!read.eof())//可耻地放弃了
//{
// read >> noskipws >> a;
// wcout << a;
// wcout.clear();//中文字符会导致输出流错误需要重置状态,以防万一加上
while (getline(read, ss))
{
for (size_t i = 0; i < ss.length(); i++)
{
a = ss[i];
wcout << a;
wcout.clear();
if (a != ' ') write << a; //[哔——]啊,还是乱码
for (size_t i = 0; i < 26; i++) //a~z
if ((int)a == i + 97)
count[i]++;
for (size_t i = 26; i < 52; i++) //A~Z
if ((int)a == i + 39)
count[i]++;
if ((int)a > 19967 && (int)a < 40870) //中文
count[52]++;
switch (a) //符号
{
case ' ':count[53]++; break;
case ',':count[54]++; break;
case ';':count[55]++; break;
case '.':count[56]++; break;
case '·':count[57]++; break;
case ',':count[58]++; break;
case ';':count[59]++; break;
case '。':count[60]++; break;
case '——':count[61]++; break;
}
}
wcout << endl;
write << endl;
}
wcout << endl;
for (size_t i = 0; i < 26; i++)
{
wcout << L"有" << count[i] << L"个小写英文字母" << wchar_t(i + 97) << "\t";
if (i % 3 == 2) wcout << endl;
}
wcout << endl;
for (size_t i = 26; i < 52; i++)
{
wcout << L"有" << count[i] << L"个大写英文字母" << wchar_t(i + 39) << "\t";
if (i % 3 == 1) wcout << endl;
}
wcout << endl;
wcout << L"有" << count[52] << L"个中文" << endl;
wcout << L"有" << count[53] << L"个空格" << endl;
wcout << L"有" << count[54] << L"个英文符号" << L"," << endl;
wcout << L"有" << count[55] << L"个英文符号" << L";" << endl;
wcout << L"有" << count[56] << L"个英文符号" << L"." << endl;
wcout << L"有" << count[57] << L"个中文符号" << L"·" << endl;
wcout << L"有" << count[58] << L"个中文符号" << L"," << endl;
wcout << L"有" << count[59] << L"个中文符号" << L";" << endl;
wcout << L"有" << count[60] << L"个中文符号" << L"。" << endl;
wcout << L"有" << count[61] << L"个中文符号" << L"——" << endl;
read.close();
write.close();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
wcout << L"readChinese运行结束。" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
return 0;
}
输出结果:英文正常,有关中文计数全部为0,输出全部乱码(原文是英文诗)