I'm trying to write a function that read a file.txt F1 and write the equivalent of each letter in another file F2 using a dictionary in a file F3. for example, the letter b = 00000010
here is what I wrote but it isn't working :/
void read_translate(){
FILE* F1;
FILE* F2;
FILE* F3;
F1 = fopen("Text.txt", "r");
F2 = fopen("Output.txt", "a+");
F3 = fopen("dictionary.txt", "r");
char ch, ch2;
int bin;
if (F1 == NULL || F2 == NULL || F3 == NULL){
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of text file are:\n");
while((ch = fgetc(F1)) != EOF){
printf("%c", ch);
fscanf(F1,"%c", &ch);
while((ch2 = fgetc(F2)) != EOF){
fscanf(F3,"%c %d", &ch2, &bin);
if(ch == ch2){
fprintf(F2,"%d", bin);
}
}
}
printf("\n");
fclose(F1);
fclose(F2);
fclose(F3);
}
here is the dictionary :
a 00000001
b 00000010
c 01000001
d 00000011
e 00000100
f 00000101
g 00000110
h 00000111
i 00001000
j 00001001
k 00001010
l 00001011
m 00001100
n 00001101
o 00001110
p 00001111
q 00010000
r 00010001
s 00010010
t 00010011
u 00010100
v 00010101
w 00010111
x 00011000
y 00011001
z 00011010
A 10000001
B 10000010
C 10000001
D 10000011
E 10000100
F 10000101
G 10000110
K 10000111
I 10001000
J 10001001
K 10001010
L 10001011
M 10001100
N 10001101
O 10001110
P 10001111
Q 10010000
R 10010001
Q 10010010
T 10010011
U 10010100
V 10010101
W 10010111
X 10011000
Y 10011001
Z 10011010
11111111
. 10011011
, 10011100
" 10011101
( 10011110
) 10011111
- 10100000
! 10100001
? 10100010
; 10100011
the output file F3 is a txt file
and here is the main.c
which is basic:
int main(){
read_translate();
return 0;
}
thank you for your help