modifier un fichier SD

Bonjour,

je ne trouve pas comment modifier un fichier SD sans le supprimer pour le re-écrire entièrement ce qui n'est pas très efficace.
Les fonction write() et print() ne semblent pas pouvoir écrire ailleurs qu'à la fin du fichier.

Merci

ci-après un exemple du pb (un peu long avec les affichages mais très simple)

#include <SPI.h>
#include <SD.h>

#define FNAME "toto"
#define FSIZE 128

File myFile;

#define SOMEWHERE 16
#define SOMETHING "something"
#define ANOTHERTHING "anotherthing"

void setup() {

  Serial.begin(115200);delay(100);
  Serial.println("start\n");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  /* create file and fill 256 bytes */
  
  SD.remove(FNAME);

  myFile=SD.open(FNAME,FILE_WRITE);
  if(!myFile){Serial.print(" OPEN WRITE KO");}
  byte c=0x01;
  for(int i=0;i<FSIZE;i++){myFile.write(c);}
  myFile.close();

  /* verify it */

  byte a[FSIZE*2];memset(a,0x32,FSIZE);  /* fill field with 0x32 to verify that 0x01 is written */             
  Serial.println("data in the field");dumpStr(a,FSIZE);
  myFile=SD.open(FNAME,FILE_READ);
  if(!myFile){Serial.print(" OPEN READ KO");}
    for(int i=0;i<FSIZE;i++){a[i]=myFile.read();}
  myFile.close();
  Serial.println("data read");
  dumpStr(a,FSIZE);


  /* try to write somewhere */

  myFile=SD.open(FNAME,FILE_WRITE);
  if(!myFile){Serial.print(" OPEN WRITE KO");}
  myFile.seek(0);
  Serial.print(" position=");Serial.print(myFile.position());
  myFile.seek(SOMEWHERE);
  Serial.print(" ; position after seek=");Serial.print(myFile.position());
  Serial.print(" ; write ");Serial.print(strlen(SOMETHING));Serial.print(" char");
  myFile.write(SOMETHING);
  Serial.print(" ; position after write=");Serial.print(myFile.position());
  myFile.close();
  Serial.println("\n");

  /* verify it */

  memset(a,0xff,FSIZE);
  myFile=SD.open(FNAME,FILE_READ);
  if(!myFile){Serial.print(" OPEN READ KO");}
    for(int i=0;i<FSIZE*2;i++){a[i]=myFile.read();}
  myFile.close();
  Serial.println("modified data read");dumpStr(a,FSIZE*2);

  /* try to print somewhere */

  myFile=SD.open(FNAME,FILE_WRITE);
  if(!myFile){Serial.print(" OPEN WRITE KO");}
  myFile.seek(0);
  Serial.print(" position=");Serial.print(myFile.position());
  myFile.seek(SOMEWHERE);
  Serial.print(" ; position after seek=");Serial.print(myFile.position());
  Serial.print(" ; print ");Serial.print(strlen(SOMETHING));Serial.print(" char");
  myFile.write(ANOTHERTHING);
  Serial.print(" ; position after print=");Serial.print(myFile.position());
  myFile.close();
  Serial.println("\n");

  /* verify it */

  memset(a,0xff,FSIZE);
  myFile=SD.open(FNAME,FILE_READ);
  if(!myFile){Serial.print(" OPEN READ KO");}
    for(int i=0;i<FSIZE*2;i++){a[i]=myFile.read();}
  myFile.close();
  Serial.println("modified data read");dumpStr(a,FSIZE*2);


  while(1){}
}

void loop() {

}

void dumpStr(byte* a,int len)
{
  for(int i=0;i<len/16;i++){
    Serial.print(i*16);Serial.print("   ");
    if(i==0){Serial.print(" ");}
    if((i*16)<100){Serial.print(" ");}
    for(int j=0;j<16;j++){  
      if(a[i*16+j]<16){Serial.print("0");}
      Serial.print(a[i*16+j],HEX);
      Serial.print(" ");
    }
    Serial.print("  ");
    for(int j=0;j<16;j++){
      byte b=a[i*16+j];
      if(b<0x20 || b>0x7e){Serial.print(".");}
      else Serial.print((char)b);
    }
    Serial.println();
  }
  Serial.println();
}

FILE_WRITE dit d'écrire à la fin du fichier

la doc de la bibliothèque SDFat supporte comme mode

O_READ - Open for reading.

O_RDONLY - Same as O_READ.

O_WRITE - Open for writing.

O_WRONLY - Same as O_WRITE.

O_RDWR - Open for reading and writing.

O_APPEND - If set, the file offset shall be set to the end of the file prior to each write.

O_AT_END - Set the initial position at the end of the file.

O_CREAT - If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, the file shall be created

O_EXCL - If O_CREAT and O_EXCL are set, open() shall fail if the file exists.

O_SYNC - Call sync() after each write. This flag should not be used with write(uint8_t) or any functions do character at a time writes since sync() will be called after each byte.

O_TRUNC - If the file exists and is a regular file, and the file is successfully opened and is not read only, its length shall be truncated to 0.