c - 在 fprintf 中格式化输出

标签 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 对齐上面所示的文本吗?谢谢。

最佳答案

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

关于c - 在 fprintf 中格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47992997/

相关文章:

Python float to string(科学记数法),特定格式

c++ - 如何在 C++ 和 R 中使用完全相同的数字格式?

wpf - StringFormat 不适用于 TextBox

data-binding - Xamarin.Forms 中标签 StringFormat 的本地化

c# - 我可以使用 C# 字符串格式和在运行时决定的输入数量吗?

c - C语言中如何检查字符串

c++ - 有人在处理 2038 time_t 错误吗?

c# - 有没有办法指示 Visual Studio 在类的底部而不是顶部自动添加字段?

c++ - 是否可以将整数的每个数字存储到 char 数组中?

c++ - VIVADO HLS 中的综合错误