passing arguments

This is a discussion on passing arguments within the C++ Programming forums, part of the General Programming Boards category; I want to execute a program from within my program rite but I want to sent some parameters to it. ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    passing arguments

    I want to execute a program from within my program rite but I want to sent some parameters to it. It is a game engine and I want to send the parameter of wether or not to open a saved game or not. Do any of u know how to do this it seems like something that can be done.
    Thanx in advace

    Kas 2002
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    Do you know how to run the program from within your program ?
    Mind mentioning the OS ?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Code:
    #include "crystal_ball.h"
    if ( vision ( divine ) ) {
      printf( "Woo Hoo!\n" );
    }

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    208

    I am using

    Windows 98 but it would be nice to have it portable though cause it is a game. Thanx for the link Salem but I read that and it didn't really help me too much
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  5. #5
    Unregistered
    Guest
    Is this what you mean? You can build up a command using sprintf, then pass the output to system().

    Get the user to input options (and check that the values are correct), or preselect them yourself. Build a command into a single string using sprintf then pass that to system().

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char string[20];
      char command[10] = "ls";
      char opt1 = 'l';
      char opt2 = 'u';
    
    
      sprintf(string, "%s -%c -%c", command, opt1, opt2);
    
      printf("The string is : %s\n\n", string);
    
      /* system(string); */
    
      return 0;
    }

  6. #6
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Arrgh! Sorry, didn't check that I was logged in, that was me. And sorry for the C instead of C++.
    This should work, but I can't compile it properly here as my compiler is older than me. Time for an upgrade methinks.
    Use:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main(void)
    {
      char string[20];
      char command[10] = "ls";
      char opt1 = 'l';
      char opt2 = 'u';
    
      sprintf(string, "%s -%c -%c", command, opt1, opt2);
    
      cout << string << endl;
    
      // Don't run this command on Dos as ls -lu is a
      // UNIX command! Substitute with whatever it is
      // you wish to run
      //
      // system(string);
    
      return 0;
    }
    Last edited by foniks munkee; 07-10-2002 at 10:05 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    208

    close

    I don't think that is what I meant because I didn't fully understand it (sorry I just woke up)
    in these code brackets I will try to show what I want

    Code:
    1. User Selects from the main menu weather they want to start  a new game or a saved game.
    2. If they chose New game execute "Game.exe"
    3. If they chose Saved Game get the filename.
    (My game program can take one command line argument and that is the filename of a saved game. If this argument is not present then it starts a new game.)
    4. Now take the name of the game they inputed and send it to my game.exe file
    5. play the game. The saved game will be opened within the game.exe file.
    sorry I prolly should have wrote sumthing like this form the beginning.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Well assuming you can manage the Question & answer session with the user (getting filenames etc), its basically

    Code:
    char game_name[100];
    char save_name[100];
    char command_string[200];
    
    // input code here
    
    if ( strlen( save_name ) > 0 ) {
        sprintf( command_string, "%s %s", game_name, save_name );
    } else {
        sprintf( command_string, "%s", game_name );
    }
    system( command_string );

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    208

    ok

    quick question

    what does sprintf(); do?
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  10. #10
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    sprintf is just like printf, only rather than sending the output to stdout it copies the formatted output to the target string.

    If you try running the programs that were given as examples, but replace the call to system() with a call to printf() or cout, you should be able to see how it works.
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main(void)
    {
      char string[30];
      char word1[] = "This";
      char word2[] = "is";
      char word3   = 'a';
      char word4[] = "string";
      int  number  = 1234;
    
      // This is were I copy all of the individual
      // strings, chars and numbers into the target
      // string, "string".
      sprintf(string, "%s %s %c %s %d!", word1, word2, word3, word4, number);
    
      cout << string << endl;
    
      return 0;
    }
    Last edited by foniks munkee; 07-12-2002 at 09:55 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    208

    so u can send

    arguments through system("game.exe -filename.dft")
    would sent the argument of -filename.dft to my program
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  12. #12
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Yes that is correct.

    Anyway the following works. To make this more dynamic though you could prompt the user for the name of the program and the appropriate arguments. But it is hardcoded here for the sake of clarity.
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main(void)
    {
      char command[30];
      char program[] = "game.exe";
      char argument[] = "filename.dft";
    
      sprintf(command, "%s -%s",program, argument);
    
      system(command);
    
      return 0;
    }

  13. #13
    Registered User
    Join Date
    May 2002
    Posts
    208

    thanx guys

    sorry about not understanding u guys there sometimes I can sorta thick
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments to the system
    By ech0wave in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 11:15 AM
  2. Passing arguments
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 12:14 PM
  3. Replies: 7
    Last Post: 11-21-2008, 03:27 PM
  4. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  5. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 07:10 AM

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