gpt4 book ai didi

c - 在 fprintf 中格式化输出

转载 作者:行者123 更新时间:2023-11-30 19:31:47 25 4
gpt4 key购买 nike

感谢您之前的帮助,我能够编写一个程序,从文本文件中的文本生成 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/

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