Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute external commands in C++/Linux?

Tags:

I just want to know which is the best way to execute an external command in C++ and how can I grab the output if there is any?

Edit: I Guess I had to tell that I'm a newbie here in this world, so I think I'm gonna need a working example. For example I want to execute a command like:

ls -la 

how do I do that?

like image 797
Lipis Avatar asked Mar 22 '09 19:03

Lipis


1 Answers

Use the popen function.

Example (not complete, production quality code, no error handling):

FILE* file = popen("ls", "r"); // use fscanf to read: char buffer[100]; fscanf(file, "%100s", buffer); pclose(file); 
like image 187
mmx Avatar answered Sep 17 '22 16:09

mmx