-
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?
-
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);
}
-
Note that strcmp returns 0 if the two strings are equal, thus the !