imbue C++ 輸入輸出漢字

來源:互聯網
上載者:User
c++裡面用L宏的時候只是說是寬字元,但是並不一定是Unicode,也就是說具體使用什麼字元集要程式員自己去設定,這就是imbue的作用,那你檔案裡不能輸入漢字也是這個問題,你只要象下面一樣設定下就可以了:
in.imbue(locale("chs"));
in>>text;
out.imbue(locale("chs"));
out<<text;

這樣C++理論上可以處理任何的字元集.

完整修改如下:
你是vc++6.0吧,那你只需要先設定後開啟檔案就可以了,這是一個已知的BUG;代碼如下:
#define _UNICODE
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
wchar_t text=L'我'; //這是調試看text沒問題,就是“我”字
wofstream out;
wifstream in;
in.imbue(locale("chs"));
in.open("input.txt");
in>>text;
in.close();

out.imbue(locale("chs"));
out.open("output.txt");
out<<text;
out.close();
wcout.imbue(locale("chs")); //如果把上面兩行去掉,下面是對的
//需要設定imbue才能輸出中文字元,不過還是不懂imbue是幹什麼的?
wcout <<"this is "<<text << endl;
system("PAUSE");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.