Outgoing file error

1

In an exercise, I need to read an input file as follows:

# add # Jabuticaba # Fruto de jabuticabeira. Jabuticabeira.
# add # Vacilar # Não estar firme. Cambalear. Oscilar. Tremer.
# add # Jacapa # Pássaro brasileiro escuro, de bico branco.
# search # Jacapa
# search # Jabuticaba
# search # Jacapa
# search # Vacilar
# sort

Where each content is between # . First, you have the operation to do: Add (add), search (search) and sort (sort). The second content is the word and the third content its meaning. For example, for the first line, what should be done is to add the word Jabuticaba to a list of words that will be sorted alphabetically at the end of the file. Remembering that one must have the meaning of the word as well. For the information, I created the following struct

typedef struct
{
    char palavra[MAX];
    char sign[MAX];
}Item;

The struct has two fields: An array to store the word itself and an array to store its meaning.

To read the file and know which operation I should perform, I used the strtok function to store only the contents of the string that is between # . Here is the function to read the file:

void LeEntrada(FILE *entrada, FILE *saida, FILE *dicionario, ArvBin *raiz, Item info)
{
    char linha[MAX];
    int i;
    char marc[3] = "#";

    if(entrada == NULL)
    {
        printf("Erro ao abrir o arquivo.\n");
        exit(1);
    }

    for(i = 0; i < 8; i++) //Faz os passos linha por linha
    {
        fgets(linha, MAX, entrada);
        char *token;

        token = strtok(linha, marc);

        if(strcmp(token, " add ") == 0)
        {
            token = strtok(NULL, marc);
            strcpy(info.palavra, token);
            Insere_Arvore(raiz, info);

            token = strtok(NULL, marc);
            strcpy(info.sign, token);
            fprintf(saida, "add%s\n", info.palavra);
        }

        else if(strcmp(token, " search ") == 0)
        {
            token = strtok(NULL, marc);
            strcpy(info.palavra, token);
            printf("%s\n", info.palavra);
            printf("%s\n", info.sign);
            Consulta_Arvore(raiz, info);
            fprintf(saida, "search%s #%s\n", info.palavra, info.sign);
        }
    }
}

There are three files as a parameter to the function: The entrada file, used for reading, a saida file, and a dicionario file. The saida file will be used to write a type of log in it, ie for each operation, I write in this file what was done. For example:

add Jabuticaba
add Vacilar
add Jacapa
search Jacapa # Pássaro brasileiro escuro, de bico branco.
search Jabuticaba # Fruto de jabuticabeira. Jabuticabeira.
search Jacapa # Pássaro brasileiro escuro, de bico branco.

However, testing with the function I did, for the saida file, I have the following result:

add Jabuticaba 
add Vacilar 
add Jacapa 
search Jacapa
 # Pássaro brasileiro escuro, de bico branco.

search Jabuticaba
 # Pássaro brasileiro escuro, de bico branco.

search Jacapa
 # Pássaro brasileiro escuro, de bico branco.

search Vacilar
 # Pássaro brasileiro escuro, de bico branco.

I do not know why after the first "search", there is a line break \n and why the content of the word meaning is repeated. What I've noticed is that it repeats the meaning of the last added word that, in this case, is Jacapa.

I would like to know the reason for this behavior and if there is any way to resolve it.

    
asked by anonymous 16.11.2017 / 19:11

0 answers