Thread: reading in command line arguments from a file?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    argc and argv are both const so you cannot modify them.

    You could call main() again with your own argc and argv buffers. This is generally a bad aproach because 1) it's not common practice, and 2) it duplicates all the variables in main().

    The better aproach is not to read from argc/v, but to store the arguements into your own strings. If no arguements are given, then default to read them from a file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    "If no arguements are given, then default to read them from a file."
    This is exactly what I'm tring to do...hence
    the if (argc==1) statement. Am I not getting something?
    and my while(fgets(arg,MAX_PATH,file)!=NULL) stores the arguments from the file into variable char arg[]. Now I dont know where to go from here. Im new to c programming and have had no experience with command line arguments. If im doing something incorrectly are poorly please correct me.

    sorry if my question was not stated clearly..
    Last edited by g1i7ch; 06-21-2006 at 06:19 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir
    argc and argv are both const so you cannot modify them.
    No they're not. Yes you can.
    C: A Reference Manual; 5th ed.
    Page 303

    When arguments are declared, those arguments are set up by the execution enviorn-
    ment and are not directly under control of the C programmer. The parameter argc is the
    count of the number of "program arguments" or "options" supplied to the program when it
    was invoked by a user or another program. The parameter argv is a vector of pointers to
    strings representing the program arguments. The first string, argv[0], is the name of the
    program; if the name is not available, argv[0][0] must be '\0'. The string argv[i],
    for i=1, ..., argc-1, is the ith program argument. Standard C requires that argv[argc]
    be a null pointer, but not so in some older implementations. The vector argv and the
    strings to which it points must be modifiable, and their values must not be changed by the
    implementation or host system during program execution. If the implementation does not
    support mixed-case strings, then the strings stored in argv must be in lower case.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Ok, after the code Ive posted dont I need to set the argc=sizeof(arg) and argv = arg or something. cuz I am lost after I open and read in the arguments into char arg[]

    my txt file has 1 line and looks like this :
    -b -s 999
    but could have more arguments in it i.e.
    -b -B 999 -s -vv -R

    printf(arg) looks just like the txt file,this is a good thing

    I dont know how to set argc and argv = to my char arg[] that contains the argument list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM