在 unicode C++ 应用程序中解析命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/332849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing command line arguments in a unicode C++ application
提问by David Reis
How can I parse integers passed to an application as command line arguments if the app is unicode?
如果应用程序是 unicode,我如何解析作为命令行参数传递给应用程序的整数?
Unicode apps have a main like this:
Unicode 应用程序有一个这样的主要内容:
int _tmain(int argc, _TCHAR* argv[])
argv[?] is a wchar_t*. That means i can't use atoi. How can I convert it to an integer? Is stringstream the best option?
argv[?] 是 wchar_t*。这意味着我不能使用 atoi。如何将其转换为整数?stringstream 是最好的选择吗?
采纳答案by Johannes Schaub - litb
if you have a TCHAR array or a pointer to the begin of it, you can use std::basic_istringstream
to work with it:
如果您有一个 TCHAR 数组或指向它开头的指针,则可以使用std::basic_istringstream
它:
std::basic_istringstream<_TCHAR> ss(argv[x]);
int number;
ss >> number;
Now, number
is the converted number. This will work in ANSI mode (_TCHAR is typedef'ed to char
) and in Unicode (_TCHAR is typedef`ed to wchar_t as you say) mode.
现在,number
是转换后的数字。这将在 ANSI 模式(_TCHAR 被 typedef'ed 到char
)和 Unicode(_TCHAR 被 typedef`ed 到 wchar_t 如你所说)模式下工作。
回答by Adam Pierce
A TCHAR is a character type which works for both ANSI and Unicode. Look in the MSDN documentation (I'm assuming you are on Windows), there are TCHAR equivalents for atoi and all the basic string functions (strcpy, strcmp etc.)
TCHAR 是一种适用于 ANSI 和 Unicode 的字符类型。查看 MSDN 文档(我假设您使用的是 Windows),atoi 和所有基本字符串函数(strcpy、strcmp 等)都有 TCHAR 等价物。
The TCHAR equivalient for atoi() is _ttoi(). So you could write this:
atoi() 的 TCHAR 等价物是 _ttoi()。所以你可以这样写:
int value = _ttoi(argv[1]);
回答by Frank Krueger
I personally would use stringstreams
, here's some code to get you started:
我个人会使用stringstreams
,这里有一些代码可以帮助您入门:
#include <sstream>
#include <iostream>
using namespace std;
typedef basic_istringstream<_TCHAR> ITSS;
int _tmain(int argc, _TCHAR *argv[]) {
ITSS s(argv[0]);
int i = 0;
s >> i;
if (s) {
cout << "i + 1 = " << i + 1 << endl;
}
else {
cerr << "Bad argument - expected integer" << endl;
}
}
回答by Sean
Dry coded and I don't develop on Windows, but using TCLAP
, this should get you running with wide character argv
values:
干编码,我不在 Windows 上开发,但是使用TCLAP
,这应该可以让您使用宽字符argv
值运行:
#include <iostream>
#ifdef WINDOWS
# define TCLAP_NAMESTARTSTRING "~~"
# define TCLAP_FLAGSTARTSTRING "/"
#endif
#include "tclap/CmdLine.h"
int main(int argc, _TCHAR *argv[]) {
int myInt = -1;
try {
TCLAP::ValueArg<int> intArg;
TCLAP::CmdLine cmd("this is a message", ' ', "0.99" );
cmd.add(intArg);
cmd.parse(argc, argv);
if (intArg.isSet())
myInt = intArg.getValue();
} catch (TCLAP::ArgException& e) {
std::cout << "ERROR: " << e.error() << " " << e.argId() << endl;
}
std::cout << "My Int: " << myInt << std::endl;
return 0;
}