1. 程式人生 > >C++輸出wchar_t和wstring

C++輸出wchar_t和wstring

首先是用wprintf_s輸出wchar_t字串,下面示例分別給出輸出wchar_t[]和wchar_t*的程式碼,在寬位元組前面要加L。

最重要的是setlocale(LC_ALL, "chs");這句話不能省略,保證能輸出中文

#include <cstring>
#include <string>
using std::locale;

const int MAXSIZE = 1024;

int main()
{
	setlocale(LC_ALL, "chs");
	wchar_t* str1 = L"This is str1";

	wchar_t str2[MAXSIZE];
	wcscpy_s(str2, MAXSIZE, L"This is str2");
	wprintf_s(L"%s\n%s\n", str1, str2);

	return 0;
}


#include <iostream>
#include <cstring>
#include <string>
using std::wcout;
using std::endl;
using std::wstring;
using std::locale;

int main()
{
	setlocale(LC_ALL, "chs");
	wstring str = L"This is str";
	wcout << str << endl;
	
	return 0;
}