在 fprintf 中格式化输出
Formatting output in fprintf
感谢您早些时候的帮助,我能够编写一个程序,从文本文件中的文本生成 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 = "$";
// sha512 hash
char * hash_sha = "$";
//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
$$l1fRKKtYSfJOVyWKw3rFH1John
$s3dCSxRBiVAzeBQR.bJLGdPL6VhH4p7fJu2aiaOLxcS4wLpWifoWNxTtaOQGnIHOZadh5rHhuuOOHEicLCV20Paul
$$.NeC/EbTUibao2by.HNV01Paul
$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1Ringo
$$CfsqYxqUQM5x0o.sjBMjV/Ringo
$$iC4vLpyDb5gAw5b7roT79LxiEd3d4LTyi5ftrWW6mwyYHNHenMiaFav4wLR3Rnmut.gJfMHn.Zy.pki9.0OBb.George
$ij/IqfPsZDlXFUEX7H82/George
$$ixjdlMMt3jg1Yb6x91HpVmV3pc9q5o8xFwejxiIyU9yZAoSsiA6qBRuHYInPyQlP4XkrnhuIyUsryLfgtldO5/
我想用fprintf来保证hash值和原来对应的词对齐在一起。例如:
文件名:mytab2411.txt
John $$l1fRKKtYSfJOVyWKw3rFH1
John $s3dCSxRBiVAzeBQR.bJLGdPL6VhH4p7fJu2aiaOLxcS4wLpWifoWNxTtaOQGnIHOZadh5rHhuuOOHEicLCV20
Paul $$.NeC/EbTUibao2by.HNV01
Paul $$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1
有人可以指导我如何使用 printf 对齐上面看到的文本吗?谢谢。
如果您将换行符添加到格式字符串“%s \n”,如果您希望键对齐,那么会将名称放在该行的开头,您可以使用 \t 作为制表符或添加特定大小到格式字符串
感谢您早些时候的帮助,我能够编写一个程序,从文本文件中的文本生成 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 = "$";
// sha512 hash
char * hash_sha = "$";
//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
$$l1fRKKtYSfJOVyWKw3rFH1John
$s3dCSxRBiVAzeBQR.bJLGdPL6VhH4p7fJu2aiaOLxcS4wLpWifoWNxTtaOQGnIHOZadh5rHhuuOOHEicLCV20Paul
$$.NeC/EbTUibao2by.HNV01Paul
$$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1Ringo
$$CfsqYxqUQM5x0o.sjBMjV/Ringo
$$iC4vLpyDb5gAw5b7roT79LxiEd3d4LTyi5ftrWW6mwyYHNHenMiaFav4wLR3Rnmut.gJfMHn.Zy.pki9.0OBb.George
$ij/IqfPsZDlXFUEX7H82/George
$$ixjdlMMt3jg1Yb6x91HpVmV3pc9q5o8xFwejxiIyU9yZAoSsiA6qBRuHYInPyQlP4XkrnhuIyUsryLfgtldO5/
我想用fprintf来保证hash值和原来对应的词对齐在一起。例如:
文件名:mytab2411.txt
John $$l1fRKKtYSfJOVyWKw3rFH1
John $s3dCSxRBiVAzeBQR.bJLGdPL6VhH4p7fJu2aiaOLxcS4wLpWifoWNxTtaOQGnIHOZadh5rHhuuOOHEicLCV20
Paul $$.NeC/EbTUibao2by.HNV01
Paul $$qQs5aHFZX/2Iaz4Y1RIihRn./AszpUZnDfld0h5mrWEtAqRJPanIO3cpT3TOOKuFS5hpmLrKAb5MY2mGV2ato1
有人可以指导我如何使用 printf 对齐上面看到的文本吗?谢谢。
如果您将换行符添加到格式字符串“%s \n”,如果您希望键对齐,那么会将名称放在该行的开头,您可以使用 \t 作为制表符或添加特定大小到格式字符串