gpt4 book ai didi

c - 从 C 语言的文本文件中读取移位密码

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

我正在研究 Shift 密码,但在加密方面遇到问题。它没有错误或编译问题,但运行后输出文件为空。我认为读取文件但未加密的 out.txt 文件是空的。我没有解决。谢谢。

int main
{

file_in = fopen("/Users/mathmoiselle/Desktop/lucky.txt", "r");

if( file_in == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}

file_out = fopen("/Users/mathmoiselle/Desktop/out.txt","r");

return 0;
}

最佳答案

根据我的评论。您需要倒回 file_in 的文件指针,而且您的包含内容顶部的格式也很糟糕。不确定这是否有什么不同(我自己是初学者,但当我读到它时肯定很突出):

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int encode (int, int);

int encode(int ch, int key) {
if (islower(ch)) {
ch = (ch-'a' + key) % 26 + 'a';
ch += (ch < 'a') ? 26 : 0;
}
else if (isupper(ch)) {
ch = (ch-'A' + key) % 26 + 'A';
ch += (ch < 'A') ? 26 : 0;
}
return ch;
}
int main (void)
{
FILE *file_in;
FILE *file_out;
char ch;
char text[300];
int key;

// gets(text); // Removed in question
file_in = fopen("shift_cipher.c", "r");


if( file_in == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("\n The contents of the file are : \n");

while( ( ch = fgetc(file_in) ) != EOF )
{
printf("%c",ch);
}

rewind(file_in);

// while (fgets(line, MAXLINE, f1)) {
// printf("%s", line);
// }

// gets(text); // Removed in question
file_out = fopen("out.txt","w");

printf("\n Enter the alphabetic offset key you would like to use:");
scanf("%d", &key);

while( ( ch = fgetc(file_in) ) != EOF )
{
printf("%c", ch);
ch=encode(ch, key);
fprintf(file_out, "%c", ch);
}
printf("file has been encoded");
fclose(file_out);
fclose(file_in);

return 0;
}

关于c - 从 C 语言的文本文件中读取移位密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512281/

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