gpt4 book ai didi

c - 如何使用 freopen 重定向输出?

转载 作者:行者123 更新时间:2023-11-30 16:37:19 24 4
gpt4 key购买 nike

目前,这是我创建的代码,但我似乎找不到其背后的问题。我正在编写一个加密程序

int countWords(FILE *f){
int count = 0;
char ch;
while ((ch = fgetc(f)) != EOF){
if (ch == '\n')
count++;
}
return count;
}

int main ( int argc, char *argv[] ){
//int min > 0;
//int max >= int min;
time_t current_time;
char* c_time_string;

current_time = time(NULL);

/* Convert to local time format. */
c_time_string = ctime(&current_time);

printf("Program started %s\n", c_time_string);


if ( argc != 2 ){
printf( "usage: %s filename\n", argv[0] );
}else {
int wordCount = 0;
FILE *file = fopen( argv[1], "r" );
//wordCount += countWords(file);
//printf("Total number words processed => %d\n", wordCount);

if ( file == 0 ){
printf( "Could not open file.\nProgram halted. Please verify the file path and try again.\n" );
}else {
int x;
while ( ( x = fgetc( file ) ) != EOF ){
printf("%c", x );
}
fclose( file );
}


/// CRYPT ///

char * plaintext = argv[1] ;
char * hash_type_1 = "$1$"; // type 1 implies md5 (number of iteration is only 1000 rounds)
char * hash_type_2 = "$6$"; // type 2 implies sha-512 (default value as in yr 2017, number of iteration is minimum 10,000 rounds )
char * salt_1 ="$"; // a simplified 0 length salt.
char * salt_2 ="ABCD1234$"; // a normal 8 character salt.
char * result;
char encyption_scheme[20]; // 20 is more than enough.

// prepare the first call using md5 and empty salt

strcpy(encyption_scheme,hash_type_1);
strcat(encyption_scheme,salt_1);
result = crypt(plaintext,encyption_scheme);
printf("MD5: %s\n",result);
// prepare the second call using sha-512 and a 8-char salt
strcpy(encyption_scheme,hash_type_2);
strcat(encyption_scheme,salt_2);
result = crypt(plaintext,encyption_scheme);
printf("Sha512: %s\n",result);
printf("Program ended %s\n", c_time_string);

//transfer the output to mytab2411.txt//
result = freopen("mytab2411.txt", "w+", stdout);

}
return 0;
}

我的编码需要一些帮助。我正在尝试编写一个程序,该程序能够将程序的输出传输到空文件。请帮我找出我的错误。

最佳答案

关于:

//transfer the output to mytab2411.txt//
result = freopen("mytab2411.txt", "w+", stdout);

对 I/O 的修改仅适用于调用后的输出。

建议将此语句放在代码“加密”部分的开头。

关于c - 如何使用 freopen 重定向输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48002129/

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