#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    strcmp function does not seemed to work


    strcmp function does not seemed to work
    result = strcmp (word, "FIREWALLGROUP=0"); this should be 0
    but it is a 1. Nowhere in the code did I add a null (\0) character to
    the array word.

    word should be equal to FIREWALLGROUP=0

    ===============================
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>

    int main()

    {

    FILE *file_pointer;
    int i = 0;
    char single_char;
    char word[15];
    int result = 99;

    /*The statement below should yield FIREWALLGROUP=0 */
    file_pointer = popen("cat /etc/yellowbox/network-config|grep FIREWALLGROUP | awk '{print $1}'", "r");

    while(!feof(file_pointer))
    {
    single_char = getc(file_pointer);
    word[i] = single_char;
    printf("i = %d %c\n", i, word[i]);
    i++;
    }

    printf("word = %s", word);
    result = strcmp (word, "FIREWALLGROUP=0");
    printf("result = %d\n", result);
    ====================================
    The output result is shown below

    root:~# ./test1
    i = 0 F
    i = 1 I
    i = 2 R
    i = 3 E
    i = 4 W
    i = 5 A
    i = 6 L
    i = 7 L
    i = 8 G
    i = 9 R
    i = 10 O
    i = 11 U
    i = 12 P
    i = 13 =
    i = 14 0
    i = 15

    i = 16 ˇ
    word = FIREWALLGROUP=0
    ˇ•ˇresult = 1
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Not being too well versed in C, I might be off base here, but here goes...

    strcmp returns the opposite of what you'd expect. If the passed variables match, 0 will be returned. 1 will be returned if they don't match.

    I think that whoever designed the function just likes to make things confusing. :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    have problem with strcmp or maybe it is my code


    result = strcmp (word, "FIREWALLGROUP=0");

    word = "FIREWALLGROUP=0"

    I expect the variable result to be a 0 not a 1.
    A valu of 0 means that they match.
    I expect them to match, but they don 't.

    Somebody help please.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    You left out the null-terminator, so word was greater than "FIREWALLGROUP=0", which is what the return value of 1 means. Your dump of word shows this; it looks like word[15] contains a '\n' and word[16] contains the EOF.

    Try this:
    Code:
    single_char = getc(file_pointer);  /* read first character */
    while(!feof(file_pointer) && single_char != '\n')
    {
        word[ i ] = single_char;
        printf("i = %d %c\n", i, word[ i ]);
        i++;
        single_char = getc(file_pointer);
    }
    word[ i ] = '\0';  /* terminate that word! 
                             replace the '\n' with '\0' */
    That should detect the new-line and terminate the string word in the proper place to get a valid compare.

    Those garbage characters that came after the newline are a side-effect of your not having terminated the string and that first one, the 'ˇ', was printed out because of how your loop was written. You reached EOF when you tried to read past the newline, but you went ahead and added it to the string and output it. You see, when EOF is reached, getc() returns the value of EOF, which is a -1, which is a character value of 0xFF or 255, which is the character 'ˇ' (check Character Map if you have access to Windows).

    Instead, you need to test for EOF before you treat the character returned by getc() as being valid. My rewrite should do that.
    Last edited by dwise1_aol; June 11th, 2003 at 03:16 PM.
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128

    Re: have problem with strcmp or maybe it is my code


    Originally posted by linh
    result = strcmp (word, "FIREWALLGROUP=0");

    word = "FIREWALLGROUP=0"

    I expect the variable result to be a 0 not a 1.
    A valu of 0 means that they match.
    I expect them to match, but they don 't.

    Somebody help please.
    Ah..sorry. Misunderstood you. Is the newline (\n) throwing off the match, maybe?

    Again, sorry if I'm off base. Just kind of throwing ideas out.

IMN logo majestic logo threadwatch logo seochat tools logo