is:示例来源。

This commit is contained in:
taynpg 2025-01-20 16:36:54 +08:00
parent 9fd98ca9a4
commit 6192d312ae

47
demo.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
//#include <io.h> //_setmode()
//#include <fcntl.h> //
#include <string>
#include "windows.h"
// source link: https://github.com/microsoft/terminal/issues/15380
int main()
{
using namespace std;
auto out_cp = GetConsoleOutputCP(); // To restore output code page at exit.
auto inp_cp = GetConsoleCP(); // To restore input code page at exit.
SetConsoleOutputCP(CP_UTF8); // Set console output code page to UTF-8 encoding.
SetConsoleCP(CP_UTF8); // Set console input code page to UTF-8 encoding.
cout << "Test: あああ🙂🙂🙂日本👌中文👍Кириллица" << endl; // Make sure you save your project file with 65001(UTF-8) encoding.
// Update: Windows console UTF-8 input has been fixed in #14745.
auto utf8 = string{};
cout << "Enter text: ";
cin >> utf8;
cout << "UTF-8 text: " << utf8 << endl;
// Outdated.
//auto wide = wstring{};
//auto utf8 = string{};
//cout << "Enter text: ";
//// stdin should be configured in order to receive wchar_t (you can't receive in UTF-8 encoding on Windows yet)
//_setmode(_fileno(stdin), _O_U16TEXT);
//wcin >> wide;
//
//// Optional: stdout should be configured to output wide strings
//_setmode(_fileno(stdout), _O_U16TEXT);
//wcout << L"Wide text: " << wide << endl;
//_setmode(_fileno(stdout), _O_TEXT); // Restore to UTF-8.
//
//// or convert wide-string to UTF-8 string before output it
//utf8.resize(wide.size() * 3); // Resize utf8 buffer for the worst case.
//auto size = WideCharToMultiByte(CP_UTF8, 0, wide.data(), (DWORD)wide.size(), utf8.data(), (DWORD)utf8.size(), 0, 0);
//utf8.resize(size);
//cout << "UTF-8 text: " << utf8 << endl;
SetConsoleOutputCP(out_cp); // Restore original system code pages.
SetConsoleCP(inp_cp); //
return 0;
}