5

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
4
  • 13
    Do you read your error messages before posting? It says the problem right there. Commented Feb 15, 2011 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. Commented Feb 15, 2011 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 Commented Feb 15, 2011 at 17:59
  • How does this have four upvotes??? Commented Jul 12, 2023 at 13:53

4 Answers 4

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.
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.

Comments

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

5 Comments

yes you right , I take your remark and from now I will write the name with ".cpp" , thanx allot
@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.
@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
@PigBen I didn't knew about .cc , Thanks
From one of the comments in stackoverflow.com/q/1545080/10358768, possible origin: cc --> C with classes and cpp --> C plus plus!
3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.