[sdFat] how to write while reading (opennext())

Hello and happy new year to all!

I do have a working function which list the content of a folder and subfolder. Then I store the path and file name of the last file read. Later i can read it and work with it content.

For debug, I need to write the parent folder content, Them I would like to write into my log file (in SD) the content of of foler.

I do not directley use Serial.print(), but I am using a "own" function

sprint(char* text, int log)

when int log is 0 => Print in terminal 1 => writein Sd 2 => both

To read the content of folder, I ma using

while (filec.openNext(repertoire, O_READ)) {
[... code..]
sprintln("test",0);
filec.close()
}

This short exemple, work fine as long as I set 0 to

sprintln("test",0);

but when I changed to

sprintln("test",2);

my while loop

while (filec.openNext(repertoire, O_READ)) {
[... code..]
sprintln("test",0);
filec.close()
}

never exist. It always display (and looping) to the first reading file. Its like if the is alway a 'rewind'

To write a file into Sd, I also need to open the target file

digitalWrite(PIN_SDLED, HIGH);
// writeFile is Global : 
// File writeFile;
      writeFile = sd.open(fileName, O_RDWR | O_CREAT | O_AT_END);
      if(writeFile)
      {
        if(ln == true)
        {
          writeFile.println(text);
        }
        else
        {
          writeFile.print(text);
        }
        
        writeFile.close();
        r = true;
      }
      else
      {
        sprint(F("\nError opening1 "),0); sprintln(fileName,0);
        r = false;
      }
      digitalWrite(PIN_SDLED, LOW);

I think, my problem is because I am writing into a my sd card, while is still reading with Opennext().

Even if I open a file [sd.open("myfile.txt", O_RDWR | O_CREAT | O_AT_END) ] with openNext(), and I close it before filec.close, it might create like a conflict

I am fuly agree that it will be hard to hellp me with the few code I pasted, but my question is:

Does someone has a exemple to write into a sd card while is still listing a folder content.

Feel you free to ask additional information or code!

I do not directley use Serial.print(), but I am using a "own" function

what's the code of that function?

also the SDFat doc states

An application which writes to a file using print(), println() or write() must close the file or call sync() at the appropriate time to force data and directory information to be written to the SD Card.

Applications must use care calling sync() sync() since 2048 bytes of I/O is required to update file and directory information. This includes writing the current data block, reading the block that contains the directory entry for update, writing the directory block back and reading back the current data block.

It is possible to open a file with two or more instances of a file object. A file may be corrupted if data is written to the file by more than one instance of a file object.

I've not checked if writing and closing a file changes the directory listing order. would be worth checking. --> list a directory. modify a file. list again. is that the same order ?