3

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
  • 863
  • 1
  • 9
  • 24
jon
  • 853
  • 3
  • 14
  • 19
  • 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
  • 1
    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

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
  • 170,119
  • 31
  • 233
  • 283
  • 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
12

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
  • 4,338
  • 6
  • 23
  • 35
Lee Netherton
  • 19,809
  • 12
  • 63
  • 99
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
  • 369,052
  • 73
  • 620
  • 1,021
Alpine
  • 3,798
  • 1
  • 24
  • 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
2

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

peoro
  • 24,831
  • 19
  • 94
  • 148