views:

174

answers:

3

I am a C++ beginner.

I'd like to create small programs that interact with the operating system (using Kubuntu Linux). So far, I have not been able to locate any tutorial or handbook to get C++ to interface with the OS.

In PHP, I can use the command exec() or the backtick operator to launch commands typically executed in a console. How can I do similar things in C++? How can I get my C++ program to execute any other command? How can I get the output of such commands?

Thanks.

+5  A: 

You can use the system() command in stdlib to execute system commands:

#include <stdlib.h>
int main() {
    system("ls -l");
}

system() returns an int as its return value, but the value of the int is system-dependent. If you try and use a command that doesn't exist, you'll get the standard "no such command" output back, and usually a non-zero return value. (For example, running system("ls -l"); on my Windows XP machine here returns a value of 1.

Stephen
Thank you for the sample code and the pointer to the proper library. That was very useful. +1
augustin
@augustin No problem. Thank you for the +1. If you would, could you please mark one of the answers here (I'd say paxdiablo's is the best) as the 'correct' answer so that this question becomes a 'solved' one? Just underneath the up/down voting arrows beside an answer, there should be the outline of a 'tick'. Click that and it should turn green. Thanks!
Stephen
I had considered marking one of the answers as the 'correct' one. I was aware of the check mark. However, the three answers were helpful and each added additional information that the other two didn't have, so I was hesitating as to which one to choose. But if it helps the system, I'll go with your recommendation. It's a shame I can choose all three :)
augustin
+7  A: 

You can use system() to execute arbitrary commands but, if you want to easily control the input and output with the program, you should look into popen().

For even more control, you can look into doing exactly what a shell might do, creating some extra file descriptors, forking to start another process, setting up file descriptors 0, 1 and 2 (input, output and error) in that process to connect them to your original process file descriptors then exec'ing the program you want to control. This isn't for the faint of heart :-)

paxdiablo
Thanks for mentioning popen(). Now that I know what to search, I have been able to find more resources on the web (e.g. http://www.gidforums.com/t-3369.html ). I'd like to soon be able to try out the stuff that "isn't for the faint of heart" :) +1
augustin
+2  A: 

You can use system() as instructed previously or you can use libraries which provide access to standard POSIX API. unistd.h and The GNU C Library include many functions for OS interaction. The possibilities with these libraries are endless as you can implement the functionalities yourself. A simple example: scan a directory for text files with scandir() and print the contents of the files.

vtorhonen
Thank you for the additional pointers to other libraries. I'm starting with something very simple, and I've bookmarked those libs for when I need to expand further what I can do. +1
augustin