Error in output file

Asked

Viewed 60 times

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 among #. First, you have the operation to be done: 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 ordered alphabetically at the end of the file. Remembering that one should have the meaning of the word too. For the information, I created the following struct

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

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

To read the file and know what operation I should perform, I used the function strtok to store only the contents of the string that is between #. Below is the function for reading 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 for the function: The file of entrada, used for reading, a file of saida and an archive dicionario. The archive saida will be used to write a log type on it, ie for each operation I write in that 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 file saida, 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 jump \n and why the content of the word’s meaning is repeated. What I noticed is that it repeats the meaning of the last word added which, in this case, is Jacapa.

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

  • Have you tried printing all the contents of the meaning array ? If the array always shows the same content it is because when recording the program goes through the array and about writes what ta la, if the program shows you the right content it is because when pulling the information, it is getting the last saved, if possible post the array print result

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.