Alex Cazacu
Alex Cazacu

Reputation: 19

Reading from a file, putting through an array and printing into file

I'm trying to make a program that translates words that are in a file and puts them in another file. Here I'm trying to read the words and put them into an array so I can search for them later and then print back into another file the translated word.

For the moment I'm trying to read and print from the array:

 #include<stdio.h>
 #include <string.h>

int main()
{
    char rom_eng[4][2], fran_eng[4][2];
    int i, j;
    FILE* re = fopen("rom_eng.txt", "r");
    FILE* out = fopen("out.txt", "w");
    if (re == NULL)
    {
        printf("Error");
        return 1;
    }
    for (i = 0; i < 2; i++)
    {
         for (j = 0; j < 4; j++)
             fscanf(re, "%s", &rom_eng);

    }

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 4; j++)
            fprintf(out, "%s \n", rom_eng);
    }
    return 0;
}

The words in the file are like this:

 - word word
 - word word

The output is the same last word repeatedly printed in the out file.

Upvotes: 0

Views: 51

Answers (1)

George Bou
George Bou

Reputation: 588

You define : char rom_eng[4][2], fran_eng[4][2]; And then you read: fscanf(re, "%s", &rom_eng);

You 're not supposed to put the '&' before rom_eng because it is defined as a char, and chars are already pointers to adresses, so in this case you dont need to put the '&' to point to the adress.

Upvotes: 1

Related Questions