【发布时间】:2014-04-18 19:37:50
【问题描述】:
我必须编写一个 C++ 程序来将命令行参数传递给 shell 脚本。 我的代码会编译,但是当我尝试使用参数运行程序时,它会开始一个新行,就像它在等待输入而不是将参数传递给脚本一样。
这是我的 C++ 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
char command[50];
int main(int argc, char *argv[])
{
sprintf(command, "%s %s", "./findName", argv[1]);
system(command);
}
我正在像这样从 unix 终端运行文件:
./findName someargument
【问题讨论】:
-
我很困惑;您是在尝试从 shell 运行 C++ 程序,还是尝试从 C++ 程序启动 shell 命令?
-
你是怎么调用c++程序的?当你在没有参数的情况下运行
findName会发生什么? -
为什么要使用固定缓冲区来寻找错误?使用
std::string。 -
@CoffeeandCode
return 0隐含在main() -
@Angew 哦,我会记住的。虽然我永远不会鼓励它。