0

I am using PIC32MX in a design made by myself and everything is working perfectly.

Now I am trying to implement a feature which is basically read from a file sequentially until I found a certain frame of characters, so I am doing:

    while( (readedBytes = FSfread((void *)&c,sizeof(char),1,ephpubData->filetouart) != 0) && G_RUNNING && ephpubData->readedBytes < 2520){
    privData->txBuffer[privData->txBufferPos++] = c;
    ephpubData->readedBytes = ephpubData->readedBytes + readedBytes;
    if (privData->txBufferPos == TX_BUFFER_SIZE){
        if (verifyDate (task) == 1){
        *gpsState = GPS_STATE_VERIFY;
        ephpubData->count++;
        break;
        }
        FSfseek(ephpubData->filetouart , ephpubData->readedBytes , SEEK_SET);
        privData->txBufferPos = 0;
   }
}

For the first time when it finds the frame (using the verifyDate function) everything is ok and it goes to break sentence. When it comes to read the second time in the while loop (after close/reopen the file and doing other things in the code) it goes to the first position again. So I want to save the the latest position found until the break sentence. I already tried to use the seek function for every while iteration

    while( (readedBytes = FSfread((void *)&c,sizeof(char),1,ephpubData->filetouart->seek) != 0) && G_RUNNING && ephpubData->readedBytes < 2520)

but it gave me an error.

scuba
  • 121
  • 4
  • If you intend to read only one character, `int ch; ch = getc( p->filepointer); ... if (ch == EOF) break;` will probably be simpler. also, you should move the `G_RUNNING && ephpubData->readedBytes < 2520) ` to the beginning of the condition, to avoid reading a character when you don't actually want it. ("short-circuiting" ) – wildplasser Dec 23 '15 at 13:08
  • Does it save the previous valid position in the file? – scuba Dec 23 '15 at 13:12
  • getc() and fgetc() read exactly one character (if there is one) sequentially, so the next call will get the next character: no need to seek. – wildplasser Dec 23 '15 at 13:15
  • So when it reach the break sentence is there any guarantee that i will read from the latest position (and not from the beginning of the file again) the next time? – scuba Dec 23 '15 at 13:24
  • Yes of course. (unless you close and reopen the file, in which case it would start again from the beginning) – wildplasser Dec 23 '15 at 14:35
  • That's the point. I need to save the exact position in the file to use it later. – scuba Dec 23 '15 at 14:48
  • ".. but it gave me an error". You may want to [edit] your post and tell us what the error message said. Unless you want to keep it a secret, of course, and you want us to guess. – Jongware Dec 23 '15 at 16:50

1 Answers1

0

Sorry folks but I found the bug.

Somewhere in the code I was doing:

    FSfseek( ephpubData->filetouart , 0 , SEEK_SET );

Which sets the reading position to 0.

scuba
  • 121
  • 4