|
-
Sep 29th, 2002, 08:28 AM
#1
Thread Starter
New Member
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?
-
Sep 29th, 2002, 10:39 AM
#2
Hyperactive Member
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);
}
-
Sep 29th, 2002, 05:43 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|