-2

I'm having trouble using strcmp in C.

I'm trying to compare a program's arguments using strcmp but even though the strings are the same it doesn't work. Here is the portion of code.

while(strcmp(argv[i], "-e") != 0)

So for i = 11 if I print the value of argv[i] I get

printf("String %s i %d", argv[i],i);

>> String -e i 11

But the while keeps on going. Any ideas why this is happening?

Code:

while(strcmp(argv[i], "-e") != 0 || i != argc)
{
    printf("String %s i %d", argv[i],i);
    if(!isdigit((unsigned char)*argv[i]) && strcmp(argv[i], "-t") != 0)
    {
        archivo = fopen(argv[i] , "r");
        TOT_IMG = TOT_IMG + 1;
        for(t=0;t<NUM_FUNC_TRAZO;t++)
        {
            for(d=0;d<NUM_FUNC_DIAMETRICA;d++)
            {
                for(c=0;c<NUM_FUNC_CIRCO;c++)
                {
                    if (fscanf(archivo, "%s",el) != EOF)
                    {
                        par->vector_circo[t][d][c] = strtod(el,NULL);
                        par->clase = clase;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        par_temp = par;
        par->siguiente = (parametros_lista) malloc(sizeof(parametros_elem));
        par = par->siguiente;
        par->anterior = par_temp;
    }
    else
    {
        if(strcmp(argv[i], "-t") != 0)
        {
            clase = atoi(argv[i]);
            CLASES = CLASES + 1;
        }
    }
    i = i + 1;
}
Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110
Atirag
  • 1,660
  • 7
  • 32
  • 60

2 Answers2

2

Let's look at this:

while(strcmp(argv[i], "-e") != 0 || i != argc)

OK, so let's assume strcmp correctly returns 0 when argv[i] is "e". We'll assume this because it's exceedingly unlikely that there's a bug in your library implementation of strcmp.

What happens if strcmp returns 0? Well, things don't just stop, your code checks whether i != argc is true. Is it? My psychic debugging skills tell me that you should look into that second part of the while.

You may also want to note that it's possible that your code could, potentially, access argv[argc], which is NULL. You may get lucky if strcmp is lenient when the input is NULL, but it's a bug that you should fix.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • 2
    I would add that `while(i < argc && strcmp(argv[i], "-e") != 0)` is probably a better choice here. – D.Shawley Mar 22 '13 at 22:09
  • hmmm ok, but I'm using "||" on the while statement so the first part of the while should act independently from the second part shouldn't it? I'm going to delete the second part to check it – Atirag Mar 22 '13 at 22:10
  • @Omri: Cool, you learn something new every day. Thanks, I'll edit accordingly. – Nik Bougalis Mar 22 '13 at 22:13
  • 1
    @Atirag think of how `||` operates - if the left hand side is false, it checks the right, if the left hand side is true, it doesn't check the right hand side. – Nik Bougalis Mar 22 '13 at 22:15
1

I'd rather recommend you to use getopt (3). This is widely used approach to parameters parsing conforming with POSIX.

Also there was another question related to achieving getopt.h interface on windows: getopt.h: Compiling UNIX C-Code in Windows. What's important it is answered (Xgetopt) so portability should be not a case.

Community
  • 1
  • 1
Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110
  • 3
    getopt is NOT a part of the C standard. Its POSIX. – cli_hlt Mar 22 '13 at 21:57
  • 2
    @cli_hlt POSIX defines the "POSIX C standard library". –  Mar 22 '13 at 21:58
  • 4
    IEEE POSIX has nothing to do with the ANSI C standard. Google will explain it to you. @Roman you should clarify your answer there. – cli_hlt Mar 22 '13 at 22:01
  • @cli_hit where did you found word 'ANSI' even in original post (OK, now I at all deleted word 'standard' to not feed trolls)? Yes, it's POSIX, isn't it standard? Especially for you: http://en.wikipedia.org/wiki/C_POSIX_library – Roman Nikitchenko Mar 22 '13 at 22:36
  • @RomanNikitchenko POSIX is a standard, it defines POSIX compliant operating systems. POSIX compliant operating systems(!) require a POSIX C library. *nix is such an OS, for example. OTOH, Windows is not. ANSI C is the standard of the C programming language. ANSI C conformant programs run on whatever system that has an ANSI C conformant compiler. ANSI C has also a standard library, which together with the language description defines the programming language C. It is very important not to mix these things up. – cli_hlt Mar 23 '13 at 17:34