File.truncate()

Hi,

I'm trying to recycle some code for handling files on an SD card. The code was written back in the days before the various functions were grouped together in some easy to use libraries.

It seems that there used to a File.truncate(unsigned long pos) function that would truncate a file from the specified position forward.

The function doesn't seem to be available in the current libraries. Is there a simple work-around? has anyone got a function to achieve this?

Thanks

SD.h is a wrapper for an old 2009 version of SdFat.

SdFat has always had truncate() but the Arduino SD.h wrapper has never allowed truncate() to be called.

the Arduino SD.h wrapper has never allowed truncate() to be called.

Why?
Is there a work-around?

I thought the general idea of an upgrade was that the new version would be in some way better/easier than the old version?

I thought the general idea of an upgrade was that the new version would be in some way better/easier than the old version?

The Official Arduino SD.h wrapper for SdFat never supported truncate(). Almost nothing has been done to fix bugs or add features to SD.h.

You could modify the SD.h wrapper to add a call to truncate() but then you will need to maintain your version of SD.h.

Here is the untested code to add to the SD.h file at about line 50 in the File class.

  boolean truncate(uint32_t length) {return _file && _file->truncate(length);}
1 Like

Thanks fatLib!

That seems to work just fine - such a simple change, such a major difference!

great work!