Error in capturing information in files using the fseek () function ... What is the problem?

0

I'm having trouble with an info capture function from a file with the fseek () function [I do not know if there is a better alternative ...]. Returning to the problem, I want to read a file that has:

"2165327575 R" "7625621 W" and so on ...

And store that number in a var int and the letter in a char and send them for treatment in another function.

But I'm doing something wrong, but I do not know what it is!

The code:

VETOR_CACHE TrataCache(VETOR_CACHE cache, CACHEDESC descricao, int *access_count, int *read_hits, int *read_misses, int *write_hits, int *write_misses)
{
    int endereco, i=0;
    char endereco_aux[100];
    char op,ch;
    FILE *arq;

    arq = fopen("input.dat", "r");
    if(arq == NULL)
        printf("Erro, nao foi possivel abrir o arquivo\n");
    else
    {
         printf("Entrou aqui 1\n");
        while( (ch=fgetc(arq))!= EOF )
        {
            fseek(arq,1,SEEK_SET);
             printf("Entrou aqui 2\n");
            while( (ch=fgetc(arq))!= 'R' ||  (ch=fgetc(arq))!= 'W' || (ch=fgetc(arq))!= '\n')
            {
                 printf("Entrou aqui while\n");
                endereco_aux[i] = ch;
                //printf("%s\n", endereco_aux);
                i++;
            }
             printf("Saiu while\n"); //Não saiu ainda...
            i=0;
            fseek(arq, 0,SEEK_CUR); //aqui ta errado eu acho
            if((ch=fgetc(arq))== 'R' ||  (ch=fgetc(arq))== 'W') //aqui ta errado ... quero percorrer e gravar os W/R 
            op = ch;    
            if( (ch=fgetc(arq))!= EOF )
            {
                endereco = atoi(endereco_aux);

                //A IDEIA É CHEGAR AQUI COM O OP E O ENDERECO E MANDAR PRA OUTRA FUNÇÃO TRATAR...
                printf("%d\n", endereco);
                printf("%s\n", op);
                *access_count = ContaLinhas();
                cache = OperaCache(true , endereco, op, cache, descricao, read_hits, read_misses, write_hits, write_misses);
            }
        }
    }
    fclose(arq);
    return cache;
}

I used these 'printf ()' to base me where the error occurred in the execution ...

    
asked by anonymous 21.11.2016 / 13:06

1 answer

1

I think your problem is more with the fgetc function than with fseek . Every time you call fgetc , you read a character and position the file pointer on the next character.
While testing if char is equal to 'R', 'W' or '\ n', you call fgets and play the result in ch , so when you exit that while ch contains 'R', 'W' or '\ n', which is the character you are interested in. In if later you call fgets again, and you lose that character, because it takes the next one. I find it easier to compare the ch you already have with '\ n', like this: if (ch != '\n') . Thus you guarantee that ch is either 'R' or is 'W', and can store in op variable.

Another comment is about fseek(arq, 0,SEEK_CUR); . This function positions the file pointer exactly where it is currently, so it does not make sense for me to call it.
Anything posts a comment here. Keep coding

    
21.11.2016 / 13:46