我正在尝试使用sprintf函数将md5Sum输出复制到字符串。但是,当我打印输出字符串时,它始终显示为零。我在这里犯了什么错误?
#include <stdio.h>
#include <string.h>
int main()
{
char command[50];
char buffer[50];
int len;
puts("Enter the password");
fgets( buffer, 50, stdin);
strcpy(command, "echo -n ");
len = strlen(buffer);
if (buffer[len - 1] == '\n')
buffer[len - 1] = '\0';
strcat(command, buffer);
strcat (command, "| md5sum");
system(command);
bzero(buffer, 50);
sprintf(buffer, "%x", system(command));
puts(buffer);
return(0);
}
输出:
elcot@boss:~$ ./md5sum
Enter the password
Karthi
51ea12796f11f1f4b72fa9316c45ead3 -
51ea12796f11f1f4b72fa9316c45ead3 -
0
您可以尝试使用“ popen”功能:
#include <stdio.h>
#include <string.h>
int main()
{
char command[50];
char buffer[50];
int len;
puts("Enter the password");
fgets( buffer, 50, stdin);
strcpy(command, "echo -n ");
len = strlen(buffer);
if (buffer[len - 1] == '\n')
buffer[len - 1] = '\0';
strcat(command, buffer);
strcat (command, "| md5sum");
//system(command);
bzero(buffer, 50);
FILE *lsofFile_p = popen(command, "r");
char *line_p = fgets(buffer, sizeof(buffer), lsofFile_p);
pclose(lsofFile_p);
printf("%s",buffer);
return(0);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句