C ++,命令行,参数 [英] C++, command line, parameters
本文介绍了C ++,命令行,参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从命令行启动c ++程序:
I am starting my c++ program from command line:
program input_file1 input_file2 output_file
其中
int main( int argc, char *argv[] )
{
short indicator= 3;
char input_file1[4096], input_file2[4096], output_file[4096];
char *p_file = NULL;
while ( --argc > 0 ) {
switch (--indicator) {
case 2:
p_file = output_file;
break;
case 1:
p_file = input_file2;
break;
case 0:
p_file = input_file1;
break;
}
for (char *argument = argv[argc]; ; ++argument) {
if (*argument == '\0')
break;
else
*p_file++ = *argument;
}
*p_file = '\0';
}
std::cout << input_file1 << '\n';
std::cout << input_file2 << '\n';
std::cout << output_file << '\n';
}
但是带有真实的论点
program D:\\data\\file1.txt D:\\data\\file2.txt D:\\data\\file3.txt
在文件名中仅存储第一个字母D ...
in names of the files only the first letter D is stored...
Output:
D
D
D
感谢您的帮助...
推荐答案
好的,所以这是简短的版本:
Ok, so here is the short version:
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout << "This program requires 1 argument!" << std::endl;
return 1;
}
std::string input_file(argv[1]);
std::cout << input_file << std::endl;
}
您应该可以从这里拿走它.
You should be able to take it from here.
这篇关于C ++,命令行,参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文