简体   繁体   English

从C ++程序调用linux命令

[英]call linux command from C++ program

I write the following simple c++ program in order to learn about how to call Linux command from C++ program (by using the system command) 我编写以下简单的c ++程序,以了解如何从C ++程序调用Linux命令(通过使用system命令)

Please advice why I have the errors from the C++ compiler? 请提出意见,为什么我会遇到C ++编译器的错误? what wrong with my program? 我的程序有什么问题?

more exm2.cc 更多exm2.cc

#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

You can't use system() without a char* parameter. 没有char*参数不能使用system()

So these statements are wrong : 所以这些陈述是错误的

system();
system();

If you are not going to make anything, just don't put anything in there. 如果您不打算做任何事情,请不要在其中放任何东西。

system() takes at one argument you could call it with an empty string: 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;
  }

But you may as well just leave those lines out :-) 但您最好也将这些行省略掉:-)

the system() function requires a parameter. system()函数需要一个参数。 Try removing the 7th and 8th line. 尝试删除第七和第八行。

#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;
}

system takes a const char* . system采用const char* You call it 5 times, passing nothing to it twice. 您调用它5次,没有两次传递任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM