is there a way to pass a variable and a string into system() at the same time?
Printable View
is there a way to pass a variable and a string into system() at the same time?
no i don't think so system() only takes a string which is usually a command.
what exactly are you trying?
Perhaps, if you used C style syntax, you could do system("commandname %s", variablename);
However, take that with a grain of salt.......I've never tried it; it's just theory on my part.
I think you want something like this:
Hope this helps :).Code:#include <stdio.h>
#include <iostream>
int main() {
char* command;
char* dir = "/home";
// this is the system command to run
sprintf(command, "ls %s", dir);
system(command);
return 0;
}
smirc:
does that coding work in c++?
As a general rule, all C code will work in C++ since C++ is meant to be a superset of C. To use C functions, you just include the name of the C header file + ".h". In the example I've included the C library stdio.h.Quote:
does that coding work in c++?
Note:
1. Usually the compiler links in stdio by default but I was jus trying to illustrate the concept. It depends on the compiler but most modern ones do this.
2. If the compiler doesn't like the char*parameter being passed to system() you'll have to cast the char* to a string.
3. I'm not sure if system is an ANSI C++ function. Can anyone clarify this?
I guess I'll read a little about C syntax.
Thx for the help, esp. smirc for the example.
Btw, about what I was tryin to do.. I have several diferent applications in mind that this problem was impeaving me from creating. One small example is a program that cleans your 'recent' dir on windows so you can watch porn and remove the evidence. lol. I completed a personal version but I wanted to make one that could act on a user name entered through cin or argv so I could distribute it to my friends w/o writing multiple copies.
eg.
system("rm c:\windoze\profiles\", usrname, "\recent\*.*");