如何将用户从控制台的输入读入 Unicode 字符串?
一个 C++ 初学者的问题.这是我目前拥有的:
A C++ beginner's question. Here is what I have currently:
// 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;
这恰好在美国有效",但我不知道如果……假设法国用户正在运行此应用程序并开始输入诸如 Çanemeplaîtpas.xml
之类的奇怪字符,会发生什么情况> 在命令行上.我正在寻找一种干净的方法来填充 CAtlString
类型的字符串.输入的最大长度总是可以设置得足够长,但理想情况下我想将 unicode 和非 unicode 条目限制为相同数量的字符.希望这样做相当简单和优雅.
This happens to "work" in the US, but I have no idea what will happen if ... say a French user is running this app and starts to enter weird characters such as Çanemeplaîtpas.xml
on command line. I am looking for a clean way to populate a string of the CAtlString
type. The maximum length of the input can always be set long enough, but ideally I would like to limit the unicode, and non-unicode entries to the same number of characters. Hopefully doing so is reasonably easy and elegant.
如果您期望 unicode 输入,您不应该使用 wcin 流吗?
Shouldn't you be using wcin stream if you expect 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;
}