System call in C++
Results 1 to 7 of 7

Thread: System call in C++

  1. #1
    Junior Member
    Join Date
    Mar 2002
    Posts
    5

    Question System call in C++

    is there a way to pass a variable and a string into system() at the same time?

  2. #2
    Member
    Join Date
    Dec 2001
    Posts
    94
    no i don't think so system() only takes a string which is usually a command.

    what exactly are you trying?

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Posts
    118
    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.

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    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.

  5. #5
    Member
    Join Date
    Dec 2001
    Posts
    94
    smirc:
    does that coding work in c++?

  6. #6
    Senior Member
    Join Date
    Oct 2001
    Posts
    638
    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.

  7. #7
    Junior Member
    Join Date
    Mar 2002
    Posts
    5

    Smile 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
  •