SD open file O_TRUNC does not work for me.

neutron7

Well-known member
I am working with Teensy 4.1 with teensyduino SD(fat) library, I can navigate folders, load, save and delete files, but one thing has me stumped.

I could be doing something wrong, but if I try to overwrite a file with new data, the file grows by 4k, and next time I open it, it still has the original data in the first 4k. If I delete the file from the SD before I re-save it, then it works as expected.

Maybe I am not properly understanding what O_TRUNC is supposed to do.

"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."

so I am doing something wrong, or it is not truncating the file as expected.

Code:
File newFile = SD.open(fullname, O_WRONLY | O_CREAT | O_TRUNC);//
for (int i = 0; i < coreBytes; i ++) {        //coreBytes = 4096
   newFile.write(coreBlock[i]);                 //coreBlock has "coreBytes" bytes
}
  Serial.println("end of coreblock");
  
  if (coreBlock[4095] > 0) { //how many paths to save
    Serial.print("Saving ");
    Serial.print(coreBlock[4095]);
    Serial.println(" paths");
etc (serial prints are for debugging, and will be removed)
 
You are using SDFat options for Open Not the SD options, which come from the class FS which is in the cores FS.h file
Your only options are:

Code:
#define FILE_READ  0
#define FILE_WRITE 1
#define FILE_WRITE_BEGIN 2

So you can use the option FILE_WRITE_BEGIN
which may not truncate the file, but then you can
do some thing newfile.truncate();
Which should truncate it at that point.
 
I just tried it, it worked. thanks!

loaded the file that had expanded itself, re saved and it went back to the proper 4096 bytes.

Code:
  File newFile = SD.open(fullname, FILE_WRITE_BEGIN);//
  newFile.truncate();
  for (int i = 0; i < coreBytes; i ++) {
    newFile.write(coreBlock[i]); //coreBlock[4096]bytes
  }
//etc

I have had no need for it, but does that mean you can not open a file for read/write?
 
Thank you!

THis was SUPER helpful, thank you so much!

I am working with Teensy 4.1 with teensyduino SD(fat) library, I can navigate folders, load, save and delete files, but one thing has me stumped.

I could be doing something wrong, but if I try to overwrite a file with new data, the file grows by 4k, and next time I open it, it still has the original data in the first 4k. If I delete the file from the SD before I re-save it, then it works as expected.

Maybe I am not properly understanding what O_TRUNC is supposed to do.

"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."

so I am doing something wrong, or it is not truncating the file as expected.

Code:
File newFile = SD.open(fullname, O_WRONLY | O_CREAT | O_TRUNC);//
for (int i = 0; i < coreBytes; i ++) {        //coreBytes = 4096
   newFile.write(coreBlock[i]);                 //coreBlock has "coreBytes" bytes
}
  Serial.println("end of coreblock");
  
  if (coreBlock[4095] > 0) { //how many paths to save
    Serial.print("Saving ");
    Serial.print(coreBlock[4095]);
    Serial.println(" paths");
etc (serial prints are for debugging, and will be removed)
 
Back
Top