How to read user input from console into Unicode string?

Starting question C ++. Here is what I have at the moment:

// From tchar.h #define _T(x) __T(x) ... // From tchar.h #define __T(x) L ## x ... // In MySampleCode.h #ifdef _UNICODE #define tcout wcout #else #define tcout cout #endif ... // In MySampleCode.cpp CAtlString strFileName; if (bIsInteractiveMode) { char* cFileName = new char[513]; tcout << endl; tcout << _T("Enter the path to a file that you would like to XYZ(purpose obfuscated) ") << endl; tcout << _T(">>> "); cin.getline(cFileName, 512); strFileName = cXmlFileName; } // Demonstrates how CAtlString can be printed using `tcout`. tcout << _T("File named '") << strFileName.GetString() << _T("' does not exist.") << endl; 

This happens with the “work” in the USA, but I don’t know what will happen if ... let's say a French user launches this application and starts typing strange characters like Çanemeplaîtpas.xml at the command line. I am looking for a clean way to populate a string like CAtlString . The maximum input length can always be set long enough, but ideally I would like to limit unicode and non-unicode entries to the same number of characters. Hope this is reasonably easy and elegant.

+4
source share
1 answer

Should you use a wcin stream if you are expecting Unicode input?

 #include <iostream> #include <string> #include <locale> int main() { using namespace std; std::locale::global(locale("en_US.utf8")); std::wstring s; std::wcin >> s; std::wcout << s; } 

+4
source

Source: https://habr.com/ru/post/1315431/


All Articles