Thread: Parameter?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    Parameter?

    Hi,

    I only make console programs, and would like to load my program like this ./programme.o -var1 value -var2 value_two

    So how can I add some parameters, and that they're put in variables when the program loads?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Those are command line arguments. You have to add the code to parse those arguments in your program. The first step is to change your main function to look like this:
    Code:
    int main(int argc, char* argv[])
    The argc variable is the number of arguments. The argv variable is an array of C style strings holding each command line argument. The command line arguments are separated by spaces, unless they are in quotes. The first argument is always the program name. So for your example: ./programme.o -var1 value -var2 value_two argc would be 5 and the contents of argv would be:
    Code:
    argv[0] -> "./programme.o"
    argv[1] -> "-var1"
    argv[2] -> "value"
    argv[3] -> "-var2"
    argv[4] -> "value_two"
    argv[5] -> NULL
    You just have to find a way to look at that data and make sense of it.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    Quote Originally Posted by Daved
    Those are command line arguments. You have to add the code to parse those arguments in your program. The first step is to change your main function to look like this:
    Code:
    int main(int argc, char* argv[])
    The argc variable is the number of arguments. The argv variable is an array of C style strings holding each command line argument. The command line arguments are separated by spaces, unless they are in quotes. The first argument is always the program name. So for your example: ./programme.o -var1 value -var2 value_two argc would be 5 and the contents of argv would be:
    Code:
    argv[0] -> "./programme.o"
    argv[1] -> "-var1"
    argv[2] -> "value"
    argv[3] -> "-var2"
    argv[4] -> "value_two"
    argv[5] -> NULL
    You just have to find a way to look at that data and make sense of it.
    Thank you, this works perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my error is storage class specified for parameter
    By meli in forum C Programming
    Replies: 5
    Last Post: 03-27-2009, 12:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM