Checking Main args

This is a discussion on Checking Main args within the C Programming forums, part of the General Programming Boards category; I have three if loops and I know when I run the program only one of them should be true ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Checking Main args

    I have three if loops and I know when I run the program only one of them should be true and execute but when I do run, it always gives me a segmentation overflow, I'm not sure where its trying to access memory beyond its capabilites.

    Code:
       if(argc == 2)
       {
            printf("enterd loop 1st loop\n");
       }
       if(argv[2][0] == '-' && argv[2][1] == 'v' && argc == 7)
       {
            printf("enterd loop 2nd loop\n");
       }   
        if(argv[2][0] == '-' && argv[2][1] == 'v' && argc < 7)
       {
            printf("enterd loop 3rd loop\n");
            exit(-1);
       }
    I can run my program in 3 different ways, the params that the 2nd and 3rd loop run fine. It is when I add the generic run method which it runs the program argv[0] and extra file param argv[1]. I know that the argc would equate to 2, but for some reason I get an error for that. Can anyone help?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there an actual argv[2]? Is there an actual argv[2][1]? Your code doesn't really show anything.

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

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    The second if would execute if the program was called in the following way:

    ./prog fname -v val1 val2 val3 val4

    The 3rd loop is to make sure that enough params are passed in.

    I don't know if this helps.

    The 1st loop is entred but for some reason I get a seg. overflow when I try to call the program by :

    ./prog fname

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    38
    If argc == 1, the test on the second loop will generate a segfault. a && b works as follows: first a is tested, if it is true b will be evaluated. Remedy: put argc check in front.
    Third loop ... :S? Maybe you should add a case argc == 1.

    Code:
       if(argc == 7 && argv[2][0] == '-' && argv[2][1] == 'v')
       {
            printf("enterd loop 2nd loop\n");
       }

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Wow, thanks. It works. I had a feeling loop2 code was causing it, becuase it dealt with the actual file. This will def. help speed up my coding now. Thanks again.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Code:
       if(argc == 2)
       {
            printf("enterd loop 1st loop\n");
       }else{   //this "else" solves your problem (in my mind..i haven't tested) 
    	//you need it because when argc is 2, then argv[2][0] doesn't exist
    
    	   //this is your code except refactored 
    	   if(argv[2][0] == '-' && argv[2][1] == 'v')
    	   {
    		if(argc == 7)
    		        printf("enterd loop 2nd loop\n");
    		else if(argc < 7)
    		{
    		        printf("enterd loop 3rd loop\n");
    		        exit(-1);
    		}
    	   }   
        }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I think I gotten it down now, the refactor code works well. Thing is I have a lot of if/else statements, so I thought making it seperate would help. The last thing is, there is this optional -k flag that can be called with the -v or just with the generic call. I'm not sure how to check for it because it makes you go through the actual program once, where as it would continue through the program until the user hits a bad key. I want to be able to check for this -k flag in all the possible location if the input is correct and then from there see what I have to do to make the program do what the flag is meant to be used for. Suggestions on this ?


    Ex:

    ./progr fname -v v1 v2 v3 v4 -k

    or

    ./prog fname -k

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-15-2008, 04:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 10:05 AM
  3. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM
  4. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 08:58 PM
  5. void or int for main?:)
    By bigtamscot in forum C Programming
    Replies: 13
    Last Post: 09-27-2001, 04:11 AM

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