1. 程式人生 > 實用技巧 >wchar_t寬字元多格式基本使用(定義輸入輸出)

wchar_t寬字元多格式基本使用(定義輸入輸出)

此資料型別主要解決漢字的問題吧。

理論方面我轉載其他優秀博主的部落格,我這裡就直接貼程式碼講解具體使用吧,

另外,寬字元錄入(輸入)的問題,我找遍全網也沒見得可以用的,

確實是有wscanf函式,但是沒有函式使用樣例和報錯解決辦法,實在讓人為難,於是自己去翻函式庫一個個找,,,才找到(原創不易啊)

程式碼如下:

#include<iostream>
#include<locale.h>
#include<stdio.h>
#include<Windows.h>
using namespace std;
locale loc("chs");
/*
//語句需要: #include<locale.h> locale loc("chs"); _wsetlocale(LC_ALL, L"chs");//或者setlocale(LC_ALL, "chs"); wcout.imbue(loc); */ int main() { _wsetlocale(LC_ALL, L"chs");//或者setlocale(LC_ALL, "chs"); wcout.imbue(loc); wchar_t s[] = L"我愛你"; wchar_t S[] = L"你愛我嗎"; /* _getws_s(s);//輸入型別1 wcin >> s;//輸入型別2
*/ wprintf(L"%ls\n", S); wprintf(L"%s\n", S); printf("%S\n", S); /* %s 和 %S 輸出區別是: %s窄字元輸出, %ls寬字元輸出, %S輸出與輸出函式相反寬度的字元格式 (也就是printf因為預設支援的寬度是單字元,而%S偏要使用相反的,那麼就 使用寬字元格式輸出,而wprintf預設支援的寬度是寬位元組,%S偏要使用相反的, 意思就是使用單字元格式輸出) */ system("pause>nul");//輸出型別1 wcout
<< s << endl; system("pause>nul");//輸出型別2 putwchar(s[2]);//或者printf("%lc",s[2]); wcout <<'\n'<< S[0] << endl; puts((s[2] == S[0])?"yes":"no"); system("pause>nul");//簡單理解使用 return 0; }
c++

詳細寬字元及其衍生函式:https://www.cnblogs.com/lanhaicode/p/10574260.html

_wsetlocale(LC_ALL, L"chs")//setlocale(LC_ALL, "chs")簡單說明及char與wchar_t轉換:https://www.cnblogs.com/zplutor/archive/2010/11/27/1889227.html

路過點個贊?