gpt4 book ai didi

将 md5sum 输出复制到 C 程序中的字符串

转载 作者:行者123 更新时间:2023-11-30 19:46:05 24 4
gpt4 key购买 nike

我正在尝试使用 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);
}

关于将 md5sum 输出复制到 C 程序中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24722285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com