Checking for command line args?

This is a discussion on Checking for command line args? within the C Programming forums, part of the General Programming Boards category; hey guys, I'm trying to search the argument line for a "-c" argument in order to execute a condition in ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Checking for command line args?

    hey guys,

    I'm trying to search the argument line for a "-c" argument in order to execute a condition in my program. This is the code i've got so far, it does not seem to be working though... any help would be greatly appreciated

    Code:
    	
    for (i=0; i <=  argc; i++){
    
    		if (argv[i] == "-c "){
    			cpresent = 1;
    		}
    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    You can't compare strings using the == operator. Try this instead:
    Code:
    if (!strcmp(argv[i], "-c")){
    If you understand what you're doing, you're not learning anything.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The normal way to do this is getopt() from unistd.h. If you are on windows, there are alternatives (nb. "getopt.h" does not really exist):

    <getopt.h> library in windows [Archive] - CodeGuru Forums
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    I tried using your suggestion itsme86 (thanks for the rationale too, i feel stupid for not remembering that) , cause i'm more familiar with strcomp, however it now gives me a seg fault...

    Code:
    	 for (i=0; i <= argc; i++){
    		if (!strcmp(argv[i], "-c")){
    			cpresent = 1;
    		}
    	}
    I also tried hard coding cpresent = 1 into the code, and it works just fine, so i'm guessing there's something wrong with that loop. Any further suggestions would be great.

    Thanks again

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    That should be:
    Code:
    for (i=0; i < argc; i++){
    Note the change from less-than-or-equal-to.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    nice, that worked perfectly, thanks

    Mind if I ask the logic behind the correction?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    argc contains the number of strings in argv, but argv is a 0-based array, therefore the last valid index of argv is argc-1. E.g. If there was one string (argc = 1), it's stored in argv[0].
    If you understand what you're doing, you're not learning anything.

  8. #8
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    You were overstepping the bounds of the array. If I'm correct the array is null terminated and you were writing over the null part causing the segfault. Putting less than fixed it because that kept you from touching that last element.

    The argv is a double pointer to character data:

    so "./my_prog -1 -2 -3" yeilds

    argc 4 meaning you have 4 elements but c arrays start at 0 so the max element = 3. You were putting things in what is the 4th slot but for a c array that is [0][1][2][3][4] <- the 5th spot.

    You cant test this with:
    Code:
    int main(int argc, char **argv)
    {
        int i = 0;
        for(i; i < argc, i++)
            printf("element %d, data %s\n", i, argv[i]);
    }
    -- Will you show me how to c++?

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    Ah perfect, thanks for that explanation, its a big help for this newb lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. Checking Main args
    By cisokay in forum C Programming
    Replies: 6
    Last Post: 05-11-2005, 01:30 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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