|
-
March 21st, 2002, 01:15 AM
#1
Junior Member
System call in C++
is there a way to pass a variable and a string into system() at the same time?
-
March 21st, 2002, 06:36 AM
#2
Member
no i don't think so system() only takes a string which is usually a command.
what exactly are you trying?
-
March 21st, 2002, 06:44 AM
#3
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.
Got Root?
This user powered by Linux.
-
March 21st, 2002, 08:42 AM
#4
I think you want something like this:
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;
}
Hope this helps .
OpenBSD - The proactively secure operating system.
-
March 21st, 2002, 10:11 AM
#5
Member
smirc:
does that coding work in c++?
-
March 21st, 2002, 10:35 AM
#6
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.
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?
OpenBSD - The proactively secure operating system.
-
March 21st, 2002, 11:04 PM
#7
Junior Member
Thanks
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\*.*");
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|