我正在研究凯撒密码,我的代码几乎完成了,但是我在从文本文件 (txt) 中读取句子、将其存储在数组中、对其进行解码以及将其再次存储在文本文件中时遇到问题。
到目前为止,我已经成功地打开了文本文件,逐行读取它,并将其显示在控制台上。
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void encode(char message[], int shift)
{
int i;
FILE *pout;
pout = fopen("OutputTrial_encode.txt", "w");
if (pout == NULL)
{
printf("File could not be opened for writing!\n");
exit(1);
}
for(i=0;i<strlen(message);i++)
{
if (!isalpha(message[i]))
continue;
// checking for upper case
if(isupper(message[i]))
message[i]=((message[i]-'A') + shift) % 26 + 'A';
else
//checking for lower case
if(islower(message[i]))
message[i]=((message[i]-'a') + shift) % 26 + 'a';
}
printf("\n%s\n", message);
fprintf(pout, "%s\n", message);
if (fclose(pout) != 0)
printf("Error in closing file!\n");
}
void decode(char message[], int shift)
{
int i;
FILE *pout;
pout = fopen("OutputTrial_decode.txt", "w");
if (pout == NULL)
{
printf("File could not be opened for writing!\n");
exit(1);
}
for(i=0;i<strlen(message);i++)
{
if (!isalpha(message[i]))
continue;
// checking for upper case
if(isupper(message[i]))
message[i]=((message[i]-'A') + (26-shift)) % 26 + 'A';
else
//checking for lower case
if(islower(message[i]))
message[i]=((message[i]-'a') + (26-shift)) % 26 + 'a';
}
printf("\n%s\n", message);
fprintf(pout, "%s\n", message);
if (fclose(pout) != 0)
printf("Error in closing file!\n");
}
void decode2(char word[], int shift)
{
int i;
FILE *pout;
//printf("Enter shift amount (1-25): ");
//scanf("%d", &shift);
pout = fopen("Output_Textfile.txt", "w");
if (pout == NULL)
{
printf("File could not be opened for writing!\n");
exit(1);
}
for(i=0;i<strlen(word);i++)
{
if (!isalpha(word[i]))
continue;
// checking for upper case
if(isupper(word[i]))
word[i]=((word[i]-'A') + (26-shift)) % 26 + 'A';
else
//checking for lower case
if(islower(word[i]))
word[i]=((word[i]-'a') + (26-shift)) % 26 + 'a';
}
printf("\n%s\n", word);
fprintf(pout, "%s\n", word);
if (fclose(pout) != 0)
printf("Error in closing file!\n");
}
int main()
{
int shift, choice1, choice2;
char message[80];
printf("Selection: \n");
printf("1. Encode/Decode\n");
printf("2. Decode Textfile\n");
printf("User input: ");
scanf("%d", &choice1);
fflush(stdin);
if(choice1==1){
printf("\nEnter message to be encrypted: ");
gets(message);
printf("Enter shift amount (1-25): ");
scanf("%d", &shift);
//fflush(stdin);
printf("\nSelection: \n");
printf("1. Encode\n");
printf("2. Decode\n");
printf("User input: ");
scanf("%d", &choice2);
switch(choice2)
{
case 1:
encode(message, shift);
break;
case 2:
decode(message, shift);
break;
}
}
else {
FILE *pin;
char filename[50], word[100];
int j=0;
do{
printf("\nEnter name of output file: ");
scanf("%30s", filename);
pin = fopen(filename, "r");
} while(pin == NULL);
if (pin == NULL)
{
printf("File could not be opened for reading!\n");
exit(1);
}
while(!feof(pin))
{
word[j] = (char) fgetc(pin);
printf("%c", word[j]);
if(feof(pin))
break;
j++;
}
printf("\nEnter shift amount (1-25): ");
scanf("%d", &shift);
decode2(word, shift);
fclose(pin);
}
printf("\n");
return 0;
}