[英]How to pass file path to program as argument if it runs from environment variable? C++
[英]How to pass an argument from a c++ program to cmd?
我正在尝试使用用户的输入作为将传递给cmd的参数...
我知道要从我的程序中使用cmd我需要使用它:
system("SayStatic.exe hello world");
但这就是我需要的:
char item[100];
gets(item);
//after getting the input I need to pass it to SayStatic.exe that is the part I dont know
我知道我不能使用sysytem(); 为此,但其他像spawnl()或execl()会工作?
首先,永远不要使用gets()
。 它不应该包含在标准库中,因为比字符串长的输入将覆盖堆栈内存并导致未定义的行为(可能是某种崩溃)。 如果您使用的是C字符串,则fgets()
是可接受的替代品。
您可以使用C ++字符串执行此操作,如下所示:
std::string line;
std::getline(std::cin, line);
system(("SayStatic.exe " + line).c_str());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.