4

I have written the following simple C++ program in order to learn how to call Linux command(s) from C++ program (by using the system command)

Please advise why I have the errors from the C++ compiler? What is wrong with my program?

more 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
Milan
  • 1,743
  • 2
  • 13
  • 36
jon
  • 903
  • 5
  • 14
  • 21
  • 13
    Do you read your error messages before posting? It says the problem **right there**. – Scott M. Feb 15 '11 at 17:44
  • none of the things you do via system() need to be done via system. See getcwd(), mkdir() etc. system() is terribly non-portable, but neatly masks that until runtime. – Flexo Feb 15 '11 at 17:48
  • 2
    I am very sorry for this but this is the first prog in C++ again sorry and thanx about your great remark – jon Feb 15 '11 at 17:59
  • How does this have four upvotes??? – Leo Jul 12 '23 at 13:53

4 Answers4

13

You can't use system() without a char* parameter.

So these statements are wrong:

system();
system();

If you are not going to make anything, just don't put anything in there.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 2
    @jon- What were you intending for the line `system();` to do? Changing it to `system("");` would get rid of the "too few arguments" error but it wouldn't necessarily make the statement useful. – bta Feb 15 '11 at 17:46
13

system() takes one argument. So, you could call it with an empty string:

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

However, you may as well just leave those lines out.

user438383
  • 5,716
  • 8
  • 28
  • 43
Lee Netherton
  • 21,347
  • 12
  • 68
  • 102
8

the system() function requires a parameter. 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;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Alpine
  • 3,838
  • 1
  • 25
  • 18
  • yes you right , I take your remark and from now I will write the name with ".cpp" , thanx allot – jon Feb 15 '11 at 18:05
  • 4
    @jon: No, he's not right. A c++ source file can have any extension you want it to. Some extensions are more common than others. ".cpp" and ".cc" are both perfectly acceptable, and both widely used. – Benjamin Lindley Feb 15 '11 at 18:13
  • @PigBen OK, thanks for your nice correction , it’s wonderful to get professional answers from the best developers in the world great and thanks again – jon Feb 15 '11 at 18:21
  • @PigBen I didn't knew about `.cc` , Thanks – Alpine Feb 15 '11 at 18:22
  • From one of the comments in https://stackoverflow.com/q/1545080/10358768, *possible origin:* `cc --> C with classes` and `cpp --> C plus plus`! – Milan Oct 18 '21 at 14:00
3

system takes a const char*. You call it 5 times, passing nothing to it twice.

peoro
  • 25,562
  • 20
  • 98
  • 150