已经经过 md5 哈希处理的字符串的 MD5 哈希:语法错误

MD5 hash of a string which is already md5 hashed: Syntax error

我想为已经经过 md5 哈希处理的字符串生成 md5 哈希。这就是我所做的!我已经循环播放了它,但不幸的是,它显示了一些错误 "sh: 2: Syntax error: "|"unexpected"。 我希望它与循环内的 "strcat" 有关。 不知何故在循环内部

strcpy(command,"echo ");
strcat(command,str);

被忽略。我迷路了!

有人能帮帮我吗?

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
   FILE *fp;
   char str[100], command[100];
   char var[100];
   int i;
   printf("Enter the string:\n");
   scanf("%[^\n]s",str);
   printf("\nString is: %s\n\n",str);
   for (i=0; i<3; i++) {
       strcpy(command,"echo ");
       strcat(command,str);
       strcat(command," | md5sum");
       strcat(command," | cut -c1-32");
       fp = popen(command, "r");
       fgets(var, sizeof(var), fp);
       pclose(fp);
       strcpy(str,var);
   }
   printf("The md5 has is :\n");
   printf("%s\n", var);   
   return 0;     

}

您的问题来自 fgets,它在读取缓冲区上保持换行。

来自 man fgets:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ([=12=]) is stored after the last character in the buffer.

所以您可能想用 [=12=] 替换 \n。您可以使用 strcspn:

   ...           
   fgets(var, sizeof(var), fp);
   pclose(fp);
   strcpy(str,var);

   /* remove first \n in str*/
   str[strcspn(str, "\n")] = '[=10=]';

   ...