2014-07-13 2 views
-1

sprintf 함수를 사용하여 md5Sum 출력을 문자열로 복사하려고합니다. 하지만 출력 문자열을 출력 할 때는 항상 출력이 0으로 표시됩니다. 내가 여기서하고있는 실수는 무엇입니까?C 프로그램에서 문자열로 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); 
    } 

출력 :

[email protected]:~$ ./md5sum 
Enter the password 
Karthi 
51ea12796f11f1f4b72fa9316c45ead3 - 
51ea12796f11f1f4b72fa9316c45ead3 - 
0 
+0

system''의 반환 값은 당신이 생각하는 것이 아니다. "0"은 "성공"을 나타냅니다. – usr2564301

+0

system (command)의 결과를 복사하는 방법은 무엇입니까? 그것을 문자 배열에 저장 하시겠습니까? –

답변

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); 
    } 
+0

그 작품에 감사드립니다. –

관련 문제