Advertisement

Calling unix commands in C++

Started by July 13, 2005 08:38 AM
2 comments, last by greystreet 19 years, 3 months ago
Is it possible to call commands like 'cp' or 'mkdir' inside C++? What I want to do is write a small program that will update my gentoo server. I know I could write this as a simple shell script but I would really like to get practice using C++. I am thinking once the program is done I can compile it and set it up in cron to update my server every month or so... Any ideas or direction for me? Thanks all, Grey
system() should work
Here

Remember to
#include <cstdlib>
instead of
#include <stdlib.h>

But these kind of things are really better done with another language, like perl or /bin/sh .
Advertisement
Since you want to do it in C rather than going through the shell, you'll want to use appropriate system calls rather than calling system()

mkdir() in <sys/stat.h>

To copy a file, you can either use link() in <unistd.h> to add a link to the file or copy the file "by hand" by creating a new file and copying the data into it.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Actually the system("command"); has worked out perfect.

I have my programs cron'd and updating once a week.

I set it up so that it pipes all of the output to a log file for further review. Very nice and easy!

Thanks for the help, really helped out alot!

Grey

This topic is closed to new replies.

Advertisement