Results 1 to 3 of 3

Thread: Compare problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    4

    Compare problem

    Hi all,

    I'm trying to compare a variable, which stores the md5sum of a file, to a variable which already has the md5sum within it. Here's what i have-

    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE *fd=popen("md5sum /home/kj/myfile","r");
    char buf[128];
    char buf2[] = "6aad283ce232c415b61e1d8c190c2b38  /home/kj/myfile";
    
    if(fd) {
    while(fgets(buf,128,fd))
           if(buf2==buf)
    	 printf("md5sum matches");
     printf("The md5sum stored in buf is : %s",buf);
    
    }
     pclose(fd);
    }
    The problem is, both the variables store the same md5sum, but it doesn't print the message , md5sum matches to the screen. What's wrong with it?

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    It is because you are comparing the pointers, not the contents of the arrays. And of course, the 2 pointers would be always different. Use string compare: strcmp();

    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE *fd=popen("md5sum /home/kj/myfile","r");
    char buf[128];
    char buf2[] = "6aad283ce232c415b61e1d8c190c2b38  /home/kj/myfile";
    
    if(fd) {
    while(fgets(buf,128,fd))
           if(!strcmp(buf2,buf))
    	 printf("md5sum matches");
     printf("The md5sum stored in buf is : %s",buf);
    
    }
     pclose(fd);
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Note that strcmp returns 0 if the two strings are equal, thus the !
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width