Mình có làm 1 bài về giải mã và mã hóa nội dung 1 file, nhưng không hiểu sao code nó không chạy đủ 2 function, mình xóa bớt 1 trong 2 đoạn code gọi lại function trong main thì cái kia vẫn chạy được. Mong mọi người giải thích hộ mình.
Code:
#include <stdio.h>
char lower[] = "abcdefghijklmnopqrstuvwxyz";
char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void Encryption(FILE *fin, FILE *fout, int shift){
int i,c;
while ((c=fgetc(fin)) != EOF){
for(i = 0; i < 26; i++){
if(upper[i] == c && (i+shift)<26) fputc(upper[i+shift], fout);
else if(lower[i] == c && (i+shift)<26) fputc(lower[i+shift], fout);
else if(upper[i] == c && (i+shift)>25) fputc(upper[i-26+shift], fout);
else if(lower[i] == c && (i+shift)>25) fputc(lower[i-26+shift], fout);
else if(c == 32){
fputc(c,fout);
break;
}
}
}
}
void Decryption(FILE *in, FILE *out, int shift){
int i,d;
while ((d=fgetc(in)) != EOF){
for(i = 0; i < 26; i++){
if(upper[i] == d && (i - shift) > -1) fputc(upper[i-shift], out);
else if(lower[i] == d && (i-shift) > -1) fputc(lower[i-shift], out);
else if(upper[i] == d && (i-shift) < 0) fputc(upper[i+26-shift], out);
else if(lower[i] == d && (i-shift) < 0) fputc(lower[i+26-shift], out);
else if(d == 32){
fputc(d,out);
break;
}
}
}
}
int main(){
FILE *fin, *fout, fout2;
char filename[20];
int shift, c;
char encrypt[] = "mahoa.txt";
char decrypt[] = "giaima.txt";
printf("Enter file's name ");
scanf("%s",filename);
printf("Enter the deviations ");
scanf("%d",&shift);
fin =fopen(filename,"a+");
if(fin == NULL){
printf("The file is not exist\n");
} else {
//encryption
fout = fopen(encrypt,"w+");
Encryption(fin, fout, shift);
fclose(fout);
fout = fopen(encrypt,"r");
fprintf(fin,"/n============================Encryption==================\n");
while ((c=fgetc(fout)) != EOF) fputc(c, fin);
printf("Encryption is successfully!\n");
//decryption
fout2 = fopen(decrypt,"w+");
Decryption(fin, fout2, shift);
fclose(fout2);
fout2 = fopen(decrypt,"r");
fprintf(fin,"\n=============================Decryption=================\n");
while ((c=fgetc(fout2)) != EOF) fputc(c, fin);
printf("Decryption is successfully!\n");
fclose(fin);
fclose(fout);
fclose(fout2);
}
return 0;
Còn đây là file đính kèm mình sử dụng: alpha.txt