The goal is to prompt the user to either encode an input file and write the code to an output file or decode an input file and write the message to an output file. Whenever I run the program, although it compiles, it stops responding as soon as the prompt character is entered.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
int encode(FILE * fp1, FILE * fp2);
int decode(FILE * fp1, FILE * fp2);
int main() {
char prompt;
printf("Would you like to encode a message into a file?\n");
printf("Or would you like to decode a message from a file?\n");
printf("Enter e to encode or d to decode.\n");
scanf("%c", prompt);
FILE *fp1, *fp2;
char filename1[INT_MAX], filename2[INT_MAX];
printf("Enter input file: ");
scanf("%s", filename1);
fp1 = fopen(filename1, "r");
printf("Enter output file: ");
scanf("%s", filename2);
fp2 = fopen(filename2, "w");
if (fp1 == NULL) {
printf("The file does not exist.\n");
return 0;
}
if (fp2 == NULL) {
printf("The file oes not exist.\n");
return 0;
}
switch (prompt) {
case 'e':
encode(fp1, fp2);
break;
case 'E':
encode(fp1, fp2);
break;
case 'd':
decode(fp1, fp2);
break;
case 'D':
decode(fp1, fp2);
break;
default:
break;
}
return 0;
}
int encode(FILE * fp1, FILE * fp2) {
char ci, co;
while (fscanf(fp1, "%c", ci) != EOF) {
for (ci = 97; ci <= 122; ci++)
co = ci - 64;
for (ci = 48; ci <= 57; ci++)
co = ci + 11;
switch (ci) {
case 32:
co = 69;
break;
case 10:
co = 70;
break;
case 13:
co = 71;
break;
default:
break;
}
fprintf(fp2, "%c", co);
}
printf("Your message has been encoded.\n");
system("pause");
return 0;
}
int decode(FILE * fp1, FILE * fp2) {
char ci, co;
while (fscanf(fp1, "%c", ci) != EOF) {
for (ci = 33; ci <= 58; ci++)
co = ci + 64;
for (ci = 59; ci <= 68; ci++)
co = ci - 11;
switch(ci) {
case 69:
co = 32;
break;
case 70:
co = 10;
break;
case 71:
co = 13;
break;
default:
break;
}
fprintf(fp2, "%c", co);
}
printf("Your message has been decoded.\n");
system("pause");
return 0;
}