Arduino Yun FileIO: Seek() then Write() overwrites whole file

Hi,

I am trying to write only a specific number of bytes to a specific position in my file on the Yun Linux SD card. I am using FileIO.h

I use Seek(pos) to point to the position pos in the file and Write(buf, len) to write len bytes.

Unfortunately the whole file is erased, with just the new data left in the file.

Does anyone know if this is what should happen?

None of the examples or tutorials show how to use Seek().

If indeed it should be able to write to specific parts of the file, without erasing it, I will review my code and post it for further help.

Thanks for your help.

What happens when you write to the file depends on how you have opened the file, not where you write in the file.

Ok so here is my test program. A seek() using file open for READ works, but open for WRITE overwrites the file, and also seems to screw the file up (when I read it I get nothing, with no errors reported).

So if you select "p" in my program, then try to read it with "r", it gives nothing.

#include <Bridge.h>
#include <FileIO.h>
#include <string.h>

char user_input;

const String sdMountPoint = "/mnt/sda1/arduino/www/";

void setup() {
  SerialUSB.begin(38400); //Open Serial connection for debugging
  while (!SerialUSB);

  Bridge.begin();
  
  // File I/O startup
  while (!FileSystem.begin());

  SerialUSB.println(F("Ready"));
}

void loop() {
  int i;
  char buf[20];
  char contents[100];

  while (SerialUSB.available()) {
    user_input = SerialUSB.read();
    
    if (user_input == 'c') {
      SerialUSB.println(F("Create file"));  
      BridgeFile dataFile = FileSystem.open(((sdMountPoint + F("test.txt")).c_str()), FILE_WRITE);
      if (!dataFile) {
        SerialUSB.println(F("Cannot open file"));
      }
      for (i = 0; i <= 30; i++) {
        dataFile.print(itoa(i, buf, 10));
        dataFile.print(","); 
      }
      dataFile.println();
      dataFile.close();   
      SerialUSB.println(F("Wrote new test.txt file with default values"));  
    } else if (user_input == 'r') {
      SerialUSB.println(F("Open file for read"));
      BridgeFile dataFile = FileSystem.open(((sdMountPoint + F("test.txt")).c_str()), FILE_READ);
      if (!dataFile) {
        SerialUSB.println(F("Cannot open file"));
      }
      i = 0;
      while (dataFile.available()) {
        contents[i++] = dataFile.read();
      }
      contents[i] = '\0';
    
      dataFile.close();
      SerialUSB.println(contents);
    } else if (user_input == 's') {
      SerialUSB.println(F("Open file for seek read"));
      BridgeFile dataFile = FileSystem.open(((sdMountPoint + F("test.txt")).c_str()), FILE_READ);
      if (!dataFile) {
        SerialUSB.println(F("Cannot open file"));
      }
      if (!(dataFile.seek(10))) {
        SerialUSB.println(F("Seek failed"));
      } else {      
        i = 0;
        while (dataFile.available()) {
          contents[i++] = dataFile.read();
        }
        contents[i] = '\0';
      }
      dataFile.close();
      SerialUSB.println(contents);
    } else if (user_input == 'p') {
      SerialUSB.println(F("Open file for seek write"));
      BridgeFile dataFile = FileSystem.open(((sdMountPoint + F("test.txt")).c_str()), FILE_WRITE);
      if (!dataFile) {
        SerialUSB.println(F("Cannot open file"));
      }
      if (!(dataFile.seek(10))) {
        SerialUSB.println(F("Seek failed"));
      } else {      
        dataFile.print(F("99999")); // dataFile.write("99999", 5) gives same result
      }
      dataFile.close();
    }
  }
  
  delay(100);
}