0

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

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
F4L4X
  • 17
  • 5
  • How many possible input letters are there? – stark Oct 29 '20 at 18:17
  • Seems related: [c - scanf() leaves the new line char in the buffer - Stack Overflow](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – MikeCAT Oct 29 '20 at 18:18
  • @stark well there is no limit – F4L4X Oct 29 '20 at 18:29
  • @stark oh sorry for my previous anwser i think it doesn't anwser your question. there are 62 inputs possible – F4L4X Oct 29 '20 at 18:44
  • You could copy the dictionary from the F2 file to an array of strings (2-dimensional `char` array), and use the characters as indexes of the array to point to each respective 8 characters binary string, like a rudimentary hash table. – isrnick Oct 29 '20 at 19:30
  • In C characters are integer numbers... Meaning that the `char` type is an integer type, it is the smallest integer type with 8 bits = 1 byte, so it can only represent a maximum of 256 distinct integer numbers. And when you assign a character like the letter `'a'` to a `char` variable what is actually stored in it is a number between 0 and 255 (look into the ASCII table for the specific number). – isrnick Oct 29 '20 at 19:30
  • So you could make a matrix like `char dictionary[256][9];` and use the characters to access each line/string of the char matrix. As an example, manually you would store the `a` character string with something like `strcpy(dictionary['a'], "00000001");`, and then you could 'translate' it doing something like `printf("%s", dictionary['a']);`. – isrnick Oct 29 '20 at 19:45
  • @isrnick thanks a lot but how will i be able to automate this as i would need to make a lot of condition like ex: if(c == a){ fprintf(F, "%c", dictionary['a']) – F4L4X Oct 30 '20 at 09:39

0 Answers0