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

Asked

Viewed 91 times

0

I’m having trouble with an Info capture function of a file with the fseek() function [I don’t know if there is a better alternative...]. Getting back 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 don’t 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 where the error occurred in the execution...

1 answer

1


I think your problem is more with function fgetc than with the fseek. Every time you call fgetc, you read a character and position the Pointer file in the next character.
No while testing whether the char is equal to 'R', 'W' or ' n', you call fgets and plays the result in ch, then when you get out of that while ch contains 'R', 'W' or ' n', which is the character you are interested in. In if further ahead you call fgets again, and ends up losing this character, because it catches the next one. It seems to me easier to compare the ch that you already have with ' n', that way: if (ch != '\n'). So you guarantee that ch or is 'R' or is 'W', and can store in variable op.

Another comment is about fseek(arq, 0,SEEK_CUR);. This function positions the file Inter exactly where it is currently, so for me it makes no sense to call it.
Anything post a comment here. Keep coding

  • Yes, I looked for another alternative and found a standardized fscanf that worked... That’s what you said, thanks.

Browser other questions tagged

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