SdFat icon indicating copy to clipboard operation
SdFat copied to clipboard

Migrating from V1 to V2 missing createContigious

Open Bambofy opened this issue 3 years ago • 2 comments

Hi, i'm am migrating my project from V1 to V2 SDFat, previously i was using SdBaseFile and SdFatEX to set up a contiguous file:

SdBaseFile myFile;
SdFatEX mySd;
bool contigFileOK = myFile.createContiguous("my file name", 1);

However the createContiguous function is not declared in the new SdFs and FsFile classes, how can i replicate these functions with the new version?

Cheers! Richard

Bambofy avatar Jan 25 '21 10:01 Bambofy

Use open() followed by preAllocate(). I prefer this sequence since you then know what failed, bad open parameters or no contiguous free space and you can reopen and truncate a file.

  // open or create file - truncate existing file.
  if (!file.open("bench.dat", O_RDWR | O_CREAT | O_TRUNC)) {
    error("open failed");
  }
  if (!file.preAllocate(FILE_SIZE)) {
      error("preAllocate failed");
  }

greiman avatar Jan 25 '21 13:01 greiman

Use open() followed by preAllocate(). I prefer this sequence since you then know what failed, bad open parameters or no contiguous free space and you can reopen and truncate a file.

  // open or create file - truncate existing file.
  if (!file.open("bench.dat", O_RDWR | O_CREAT | O_TRUNC)) {
    error("open failed");
  }
  if (!file.preAllocate(FILE_SIZE)) {
      error("preAllocate failed");
  }

Cheers! i'll try this out

Bambofy avatar Jan 25 '21 13:01 Bambofy