file.seek() works intermittently

Hello forum,

I'm using Catalex v1.0 SD reader in a device that counts our livestock. The unit reads their RFID's, checks each number against an index file, and if the RFID number is found, it pulls the animals other data from the fields in this file and drops it into a new count file, which is uploaded to my FTP server once the count is done.

The fields of the index file looks like this:

982000421306247,0,6247,HL,0,!
982000421306250,0,D177,HK,0,!
982000421306252,0,6252,LK,0,!

The second field is '0' or '1', if zero, it means this animal has not been counted during this session. As soon as it passes through, the zero is changed to one, so that if it manages to pass back up the chute the counter will see a one in its record, and ignore the animal so it does not get counted twice and mess up the tally.

Now the issue I'm having is that I can file.seek() to anywhere and read any value from the file quite ok, but when I file.seek() and attempt to write a new value (like changing zero to one) the new value is placed at the end of the file.

What am I doing wrong here? In the attached simplified sketch I'm attempting to change the first ,0, to ,1,. I know the file.seek() works when I simply read a value, as can bee seen from "FILE POSITION #47 HAS VALUE OF '0'." I'm using SdFat by Bill Greimann v.1.1.0

Sketch:

 void seekAndEdit()
    {
      printFile();     
      tempFile = SD.open(tempFilename, FILE_WRITE);     
      unsigned long pos = 16;
      tempFile.seek(pos);
      byte duplicate = tempFile.read();    
      Serial.print(F("FILE POSITION #"));
      Serial.print(pos);
      Serial.print(F(" HAS VALUE OF '"));
      Serial.print(char(duplicate));
      Serial.println(F("'. NOW CHANGING VALUE..."));
      Serial.println();

      if (duplicate == '0')
      {
        tempFile.seek(pos);
        tempFile.print('1');
      }

      if (duplicate == '1')
      {
        tempFile.seek(pos);
        tempFile.print('0');
      }
      tempFile.close();
      printFile();     
    }


  void printFile()
    {
      tempFile = SD.open(tempFilename, FILE_READ);
      Serial.println(F(">>> FILE BEGIN"));
      for (int i = 0; i < tempFile.size(); i++)
      {
        tempFile.seek(i);
        Serial.print(char(tempFile.read()));
      }
      Serial.println();
      Serial.println(F(">>> FILE END"));
      Serial.println();
      tempFile.close();
    }

And the serial output:

<<< INITIALISING SD CARD... >>>
>>> FILE BEGIN
982000421306247,0,6247,HL,0,!
982000421306250,0,D177,HK,0,!
982000421306252,0,6252,LK,0,!
>>> FILE END

FILE POSITION #16 HAS VALUE OF '0'. NOW CHANGING VALUE...

>>> FILE BEGIN
982000421306247,0,6247,HL,0,!
982000421306250,0,D177,HK,0,!
982000421306252,0,6252,LK,0,!1
>>> FILE END

This is what it looks like, but somehow I doubt the type of breakout has anything to do with the issue.

Found the solution as per SD: seek and write seems not working! - Programming Questions - Arduino Forum

I tried this solution earlier today but it caused my system to hang - so I left it there. Turns out I had the syntax wrong.

When I initialised the file like this: tempFile = SD.open(tempFilename, (O_READ | O_WRITE)); it works fine.

1 Like