格式化fprintf中的输出

时间:2017-12-27 13:23:50

标签: c formatting string-formatting

感谢您之前的帮助,我能够编写一个程序,从文本文件中的文本生成SHA 512和MD5哈希,并将输出哈希值放在单独的文本文件中。我非常感激。 该计划如下:

文件名:hashgen.c

#include <stdio.h>
#include <string.h>
#include <crypt.h>
#include <stdlib.h>
#include <time.h>


int main (){

    // Start : Display the time of starting the program
    time_t starttime;
    struct tm * starttimeinfo;

    time ( &starttime );
    starttimeinfo = localtime ( &starttime );
    printf ( "Program Started At: %s", asctime (starttimeinfo) );
        //declaring a file to be selected by user
    FILE *user_file;
    char buf[1000];

    FILE *resultfile;
    resultfile = fopen("mytab2411.txt","w");


    // md5 hash
    char * hash_md5 = "$1$";
    // sha512 hash
    char * hash_sha = "$6$";

    //Specify Salt   
    char * salt_1 ="$";     
    char * result;
    char encyption_scheme[20];
    char userfilename[128];

    //Prompt to enter file name
     printf("Enter a file name\n");
     scanf("%123s",userfilename);

    //opening the file  
    user_file=fopen(userfilename,"r"); 
    //Error if file does not exist
    if (!user_file){
        printf("Could not find file: %123s",userfilename);
        printf("Please verify the file path");


        time_t exittime1;
        struct tm *exittimeinfo1;

        time ( &exittime1 );
        exittimeinfo1 = localtime ( &exittime1 );


        printf ( "Program Ended At: %s", asctime (exittimeinfo1) );
            return (EXIT_FAILURE);
            }
        //reading from file
        while (fgets(buf,1000, user_file)!=NULL){

        /* hashing using md5 */

            strcpy(encyption_scheme,hash_md5);
            strcat(encyption_scheme,salt_1);
            result = crypt(buf,encyption_scheme);
            fprintf(resultfile,"%s",buf);
            fprintf(resultfile,"%s",result);

             /* hashing using sha-512 */ 
             strcpy(encyption_scheme,hash_sha);
             strcat(encyption_scheme,salt_1);
             result = crypt(buf,encyption_scheme);
            fprintf(resultfile,"%s",buf);

            fprintf(resultfile,"%s",result);
            }
            //closing file
            fclose(resultfile);
            //display time program ended
            time_t endtime;
            struct tm * endtimeinfo;

            time ( &endtime );
            endtimeinfo = localtime ( &endtime );


            printf ( "Program Ended At: %s", asctime (endtimeinfo) );

}

使用的文本文件包含以下词语:

文件名:Sample.txt

John
Paul
Ringo
George

现在,Sample.txt中单词的哈希值存储在mytab2411.txt中。该文件显示为。

文件名:mytab2411.txt

John
$1$$l1fRKKtYSfJOVyWKw3rFH1John
$6$$9s3dCSxRBiVAzeBQR.bJLGdPL6VhH4p7fJu2aiaOLxcS4wLpWifoWNxTtaOQGnIHOZadh5rHhuuOOHEicLCV20Paul
$1$$.NeC/EbTUibao2by.HNV01Paul
$6$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1Ringo
$1$$CfsqYxqUQM5x0o.sjBMjV/Ringo
$6$$iC4vLpyDb5gAw5b7roT79LxiEd3d4LTyi5ftrWW6mwyYHNHenMiaFav4wLR3Rnmut.gJfMHn.Zy.pki9.0OBb.George
$1$$2ij/IqfPsZDlXFUEX7H82/George
$6$$ixjdlMMt3jg1Yb6x91HpVmV3pc9q5o8xFwejxiIyU9yZAoSsiA6qBRuHYInPyQlP4XkrnhuIyUsryLfgtldO5/

我想使用fprintf来确保哈希值和原始对应的单词对齐在一起。例如:

文件名:mytab2411.txt

John $1$$l1fRKKtYSfJOVyWKw3rFH1    
John $6$$9s3dCSxRBiVAzeBQR.bJLGdPL6VhH4p7fJu2aiaOLxcS4wLpWifoWNxTtaOQGnIHOZadh5rHhuuOOHEicLCV20
Paul $1$$.NeC/EbTUibao2by.HNV01
Paul $6$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1

有人可以指导我如何使用printf对齐上面的文本吗?谢谢。

1 个答案:

答案 0 :(得分:0)

如果将换行添加到格式字符串&#34;%s \ n&#34;如果您希望键对齐,则可以将名称放在行的开头,您可以使用\ t作为制表符或将特定大小添加到格式字符串