我编写了以下简单的c ++程序,以了解如何从C++程序调用Linux命令(使用system命令)
请告诉我为什么我有C++编译器的错误?我的程序有什么问题?
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("echo -n '1. Current Directory is '; pwd");
system("mkdir temp");
system();
system();
system("echo -n '3. Current Directory is '; pwd");
return 0;
}
[root@linux /tmp]# g++ -Wall exm2.cc -o exm2.end
/usr/include/stdlib.h: In function ?int main()?:
/usr/include/stdlib.h:738: error: too few arguments to function ?int system(con?
exm2.cc:7: error: at this point in file
/usr/include/stdlib.h:738: error: too few arguments to function ?int system(con?
exm2.cc:8: error: at this point in file
Run Code Online (Sandbox Code Playgroud)
system()没有char*参数就不能使用.
所以这些陈述是错误的:
system();
system();
Run Code Online (Sandbox Code Playgroud)
如果你不打算做什么,就不要在里面放任何东西.
system()接受一个参数,你可以用空字符串调用它:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("echo -n '1. Current Directory is '; pwd");
system("mkdir temp");
system("");
system("");
system("echo -n '3. Current Directory is '; pwd");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但你可以把这些线路留下来:-)
该system()函数需要一个参数.尝试删除第7行和第8行.
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("echo -n '1. Current Directory is '; pwd");
system("mkdir temp");
system("echo -n '3. Current Directory is '; pwd");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是system()文件名?
一个c ++程序源文件有一个扩展,system()所以它应该system()
是不是吗?