Thread: proplem searching runtime arguments

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    proplem searching runtime arguments

    The program compiles fine but windows keeps giving me errors when I run the program:

    Code:
    while (t<=argc){
    	  if (argv[t][0]=='-'){
    		  printf("Found option");
    	  }
    	  t++;
      }
    Last edited by angelfly; 04-28-2002 at 02:18 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You've exceeded the array bounds....

    try

    Code:
    while (t<argc){
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70
    consider:
    C:\>prog A: -s -t

    executes the job(prog) to the file or device A: with that options -s & -t.

    for each [argc] is the number of arguments passed to prog; that are 3 (A:, -s, -t);

    argv[argc] points to the first element of the argument. if argc is NULL the program name is not available,

    so<?> :
    consider t = 1,
    argv[t] will point to the first elemet of "A:" string that's 'A'.

    so if t increases t = 2, points the first element of "-s" that's '-'

    use <string.h> functions to identify which letter was used after the dash('-') to apply the option
    Ünicode¬>world = 10.0£

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    thanks it works now.I used <= because I thought if I used < and for example there were 5 arg it would stop at 4

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Thats the funny thing with array indexes ! If there are 5 elements you access them using ary[0] thru to ary[4] (0 to 4 makes the 5 elements).

    So, the following defines and populates an array of 5 ints, with the values 10 - 14:

    Code:
    #define MAX_ELEMENTS 5
    
    int ary[MAX_ELEMENTS];
    int i;
    
    for (i = 0; i < MAX_ELEMENTS; i++)
    {
      ary[i] = i +10;
    }
    Also, the argc/argv combo people use with main includes the program name as the first element, then the command line args in the remainder. So if you're looking for switches (-t -i etc), you'd best start with argv[1], not argv[0].
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM