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,

>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;
std::system(s.str().c_str());

so, in other words I'm better off keeping it a shell script?

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

C code:

int x = 9;
char s[100];

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

well, I'll try that, thanks guys.

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

forget it, %s is the happy solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.