I'm trying to re-write some of my shell scripts with C++ and I want to use some Unix commands, but I don't really get how, all the information on System() is just confusing to me...so any help would be great,

11 Years
Discussion Span
Last Post by Subterraneus

>all the information on System() is just confusing to me
It's just a command interpreter. Pass it a string just like you would your shell and things will happen. :rolleyes: Intuitively, system is one of the simplest functions.


If your commands produce output consider using popen() which works like somewhat system() but allows one-way communication between the shell and the C program.

system() does'nt do that.

man popen


well, no output that I want or need, so system() is fine, but I can't seem to use variables with it. for example:

int doodle = 9;
system(echo doodle);

ehich just returns either an error about echo not being a valid variable or it just echos doodle.
*shrugs* maybe I just don't get it...


system expects the memory address of an array af characters. Your code is not valid C++ syntax. You could write system("echo 9"); . Or you could use a stringstream to build a string dynamically.

std::ostringstream s("");
int x(9);
s << "echo " << x;

No, you just have to pass system() a character array.

C code:

int x = 9;
char s[100];

sprintf(s, "echo %i", x);

well, this is all fine and dandy, until I want to use this with something like

"nicl . -create /users/%i", x

the, when I use char x[50] instead of int(so I can use a username), I just get that nicl is being given a bad path (and I used a valid name in my C++)

NOTE: I am working in Mac OS X

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.