嗨我有一個文件包含日文文本,保存爲unicode文件。無法從文件讀取unicode(日語)
我需要從文件中讀取並將信息顯示到標準輸出。
我使用Visual Studio 2008
int main()
{
wstring line;
wifstream myfile("D:\sample.txt"); //file containing japanese characters, saved as unicode file
//myfile.imbue(locale("Japanese_Japan"));
if(!myfile)
cout<<"While opening a file an error is encountered"<<endl;
else
cout << "File is successfully opened" << endl;
//wcout.imbue (locale("Japanese_Japan"));
while (myfile.good())
{
getline(myfile,line);
wcout << line << endl;
}
myfile.close();
system("PAUSE");
return 0;
}
該程序產生一些隨機的輸出,我看不到屏幕上的任何日語文本。
實際上,wfstream已經使用了一個專門用於wchar_t的basic_filebuf,事實上,如果情況並非如此,pubsetbuf將會失敗。所以這段代碼除了將緩衝區縮小爲128個寬字符外別無其他。 – bdonlan
@bdonlan wfstream使用的是wfilebuf,它是basic_filebuf。默認使用char緩衝區,正如我提供的鏈接中所解釋的那樣。 –
ymett