Passing variables to system

This is a discussion on Passing variables to system within the C Programming forums, part of the General Programming Boards category; Id like to know how to pass variables to system, for example Code: system(program %d, variable); Im not sure how ...

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Unhappy Passing variables to system

    Id like to know how to pass variables to system, for example

    Code:
     system(program %d, variable);
    Im not sure how to do this.

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You'll want to use something like sprintf() to do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Here's how I do it. I load a string with the entire command and pass it to the system call.

    sprintf(sysstr,"program_name %d",variable);
    system(sysstr);

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Or even better yet...

    Example:
    Code:
    #define BUFFERSIZE  4096 // 4k buffer should suffice.
    
    int vsystem(const char *fmt, ...)
    {
      va_list args;
      char buffer[BUFFERSIZE];
    
      args = va_start(args, fmt);
      /* A word of warning. This is kind of where it helps to look into a more buffer friendly version of
       * vsprintf() since there are issues when you do things like this. Microsoft has vsprintf_s() which
       * is more advisable to use than vsprintf() but you sacrifice portability then.
       */
      if(vsprintf(buffer, fmt, args) >= BUFFERSIZE)
        fprintf(stderr, "A buffer overflow probably already crashed the program. If not, just know that it did happen.");
      va_end(args);
    
      return system(buffer);
    }
    Now you can do nifty things like:

    Example:
    Code:
    vsystem("gcc %s.c -c -o %s.o", user_input, user_input);

  5. #5
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    nice, solved it, thanks ^^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Variables
    By scootsonline in forum Windows Programming
    Replies: 2
    Last Post: 09-14-2006, 10:11 AM
  2. Passing string variables
    By koolsafe in forum C Programming
    Replies: 4
    Last Post: 03-11-2006, 01:45 AM
  3. Putting variables in SYSTEM
    By mycro in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2003, 08:59 PM
  4. Passing system time to a function
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 01:56 PM
  5. passing a parameter to a system invoke
    By iain in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2001, 10:21 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21