Thread: Must you concatenate strings before using them in commands such as system()?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    31

    Must you concatenate strings before using them in commands such as system()?

    I'm trying to get the effect of 2 char *'s in one, like so:

    Code:
    //I have these varibles...
    char *exepath="c:\\test\\";
    char *exe="test.exe";
    //and based on them, id like to run this command:
    system("c:\\test\\test.exe");
    //however I'd like to do it based on what values the variables have. Whats the easiest way to do this? I know theres no way in C.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use std::string
    Code:
    std::string path("C:\\test\\");
    std::string exe("test.exe");
    std::string fullPath = path+exe;
    system(fullPath.c_str());
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What do you mean "there's no way in C"? Of course there is!
    All you need is an actual buffer to store the concatenation result in, and "strcat".
    But since this is the C++ forum, go with vart's example.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. system commands
    By darealnash in forum C++ Programming
    Replies: 8
    Last Post: 08-25-2004, 07:20 PM
  2. Invoking system commands... "A better way?"
    By The_Muffin_Man in forum C++ Programming
    Replies: 10
    Last Post: 06-10-2004, 04:57 AM
  3. what can i type into msdn to bring up all the system commands
    By Shadow12345 in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2002, 06:58 PM
  4. Capturing system commands
    By manwhoonlyeats in forum C Programming
    Replies: 2
    Last Post: 12-07-2002, 07:38 PM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM