[英]C++ on Windows: executable path with whitespace in system() call
[英]Cannot find executable in path for Visual C++ system call
我正在尝试在Visual C ++程序中运行ping
。
在命令行中,键入where ping
返回C:\\Windows\\System32\\PING.exe
。 此外,当我从命令行发出ping
命令时,它可以在任何目录下工作。 在我看来,这就是我的道路。
但是,当我尝试运行以下C ++程序时,出现错误。
#include <process.h>
#include <iostream>
int main()
{
int pingretval = system("ping stackoverflow.com > /dev/null");
std::cout << "Ping returns " << pingretval << std::endl;
return 0;
}
控制台输出:
The system cannot find the path specified.
Ping returns 1
如何使system()
选择正确的可执行文件?
编辑:我在system
参数C:\\\\Windows\\\\System32\\\\PING.exe
ping
更改为C:\\\\Windows\\\\System32\\\\PING.exe
,但仍然会出现相同的错误。
编辑2:所以我不是完全诚实。 诚挚的歉意。 在我的系统调用中,我将重定向到/dev/null
Linux样式。 删除该位后,我忘记了重新编译,并且Windows文件系统中/dev/null
是“找不到指定的路径”错误的原因。 通过删除> /dev/null
,就可以了。
对于Windows,不要重定向到/dev/null
。
不幸的是,在我的实际应用程序中,我无法更改ping -c 3 stackoverflow.com > /dev/null
形式的系统调用参数。 我试图通过对ping
进行封装以使其忽略而忽略Windows -c
开关(仅适用于Linux),但试图使它适应Windows,但我忘记更改Linux样式重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.