utility
SdFile Class Reference
Access FAT16 and FAT32 files on SD and SDHC cards. More...
#include <SdFat.h>
Public Member Functions | |
| SdFile (void) | |
| Create an instance of SdFile. | |
| void | clearUnbufferedRead (void) |
| writeError is set to true if an error occurs during a write(). | |
| uint8_t | close (void) |
| Close a file and force cached data and directory information to be written to the storage device. | |
| uint8_t | contiguousRange (uint32_t *bgnBlock, uint32_t *endBlock) |
| Check for contiguous file and return its raw block range. | |
| uint8_t | createContiguous (SdFile *dirFile, const char *fileName, uint32_t size) |
| Create and open a new contiguous file of a specified size. | |
| uint32_t | curCluster (void) const |
| uint32_t | curPosition (void) const |
| uint32_t | dirBlock (void) const |
| uint8_t | dirEntry (dir_t *dir) |
| Return a files directory entry. | |
| uint8_t | dirIndex (void) const |
| uint32_t | fileSize (void) const |
| uint32_t | firstCluster (void) const |
| uint8_t | isDir (void) const |
| uint8_t | isFile (void) const |
| uint8_t | isOpen (void) const |
| uint8_t | isSubDir (void) const |
| uint8_t | isRoot (void) const |
| void | ls (uint8_t flags=0, uint8_t indent=0) |
| List directory contents to Serial. | |
| uint8_t | makeDir (SdFile *dir, const char *dirName) |
| Make a new directory. | |
| uint8_t | open (SdFile *dirFile, uint16_t index, uint8_t oflag) |
| Open a file by index. | |
| uint8_t | open (SdFile *dirFile, const char *fileName, uint8_t oflag) |
| Open a file or directory by name. | |
| uint8_t | openRoot (SdVolume *vol) |
| Open a volume's root directory. | |
| int16_t | read (void) |
| Read the next byte from a file. | |
| int16_t | read (void *buf, uint16_t nbyte) |
| Read data from a file starting at the current position. | |
| int8_t | readDir (dir_t *dir) |
| Read the next directory entry from a directory file. | |
| uint8_t | remove (void) |
| Remove a file. | |
| void | rewind (void) |
| Set the file's current position to zero. | |
| uint8_t | rmDir (void) |
| Remove a directory file. | |
| uint8_t | rmRfStar (void) |
| Recursively delete a directory and all contained files. | |
| uint8_t | seekCur (uint32_t pos) |
| Set the files position to current position + pos. | |
| uint8_t | seekEnd (void) |
| Set the files current position to end of file. | |
| uint8_t | seekSet (uint32_t pos) |
| Sets a file's position. | |
| void | setUnbufferedRead (void) |
| Use unbuffered reads to access this file. | |
| uint8_t | timestamp (uint8_t flag, uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) |
| Set a file's timestamps in its directory entry. | |
| uint8_t | sync (void) |
| The sync() call causes all modified data and directory fields to be written to the storage device. | |
| uint8_t | type (void) const |
| Type of this SdFile. | |
| uint8_t | truncate (uint32_t size) |
| Truncate a file to a specified length. | |
| uint8_t | unbufferedRead (void) const |
| SdVolume * | volume (void) const |
| size_t | write (uint8_t b) |
| Write a byte to a file. | |
| size_t | write (const void *buf, uint16_t nbyte) |
| Write data to an open file. | |
| size_t | write (const char *str) |
| Write a string to a file. | |
| void | write_P (PGM_P str) |
| Write a PROGMEM string to a file. | |
| void | writeln_P (PGM_P str) |
| Write a PROGMEM string followed by CR/LF to a file. | |
| uint8_t | contiguousRange (uint32_t &bgnBlock, uint32_t &endBlock) |
| uint8_t | createContiguous (SdFile &dirFile, const char *fileName, uint32_t size) |
| uint8_t | dirEntry (dir_t &dir) |
| uint8_t | makeDir (SdFile &dir, const char *dirName) |
| uint8_t | open (SdFile &dirFile, const char *fileName, uint8_t oflag) |
| uint8_t | open (SdFile &dirFile, const char *fileName) |
| uint8_t | open (SdFile &dirFile, uint16_t index, uint8_t oflag) |
| uint8_t | openRoot (SdVolume &vol) |
| int8_t | readDir (dir_t &dir) |
Static Public Member Functions | |
| static void | dateTimeCallback (void(*dateTime)(uint16_t *date, uint16_t *time)) |
| Set the date/time callback function. | |
| static void | dateTimeCallbackCancel (void) |
| Cancel the date/time callback function. | |
| static void | dirName (const dir_t &dir, char *name) |
| Format the name field of dir into the 13 byte array name in standard 8.3 short name format. | |
| static void | printDirName (const dir_t &dir, uint8_t width) |
| Print the name field of a directory entry in 8.3 format to Serial. | |
| static void | printFatDate (uint16_t fatDate) |
| Print a directory date field to Serial. | |
| static void | printFatTime (uint16_t fatTime) |
| Print a directory time field to Serial. | |
| static void | printTwoDigits (uint8_t v) |
| Print a value as two digits to Serial. | |
| static uint8_t | remove (SdFile *dirFile, const char *fileName) |
| Remove a file. | |
| static void | dateTimeCallback (void(*dateTime)(uint16_t &date, uint16_t &time)) |
| static uint8_t | remove (SdFile &dirFile, const char *fileName) |
Detailed Description
Access FAT16 and FAT32 files on SD and SDHC cards.
Definition at line 135 of file SdFat.h.
Constructor & Destructor Documentation
Member Function Documentation
| void clearUnbufferedRead | ( | void | ) |
| uint8_t close | ( | void | ) |
Close a file and force cached data and directory information to be written to the storage device.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include no file is open or an I/O error.
Definition at line 74 of file SdFile.cpp.
| uint8_t contiguousRange | ( | uint32_t * | bgnBlock, |
| uint32_t * | endBlock | ||
| ) |
Check for contiguous file and return its raw block range.
- Parameters:
-
[out] bgnBlock the first block address for the file. [out] endBlock the last block address for the file.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include file is not contiguous, file has zero length or an I/O error occurred.
Definition at line 91 of file SdFile.cpp.
| uint8_t contiguousRange | ( | uint32_t & | bgnBlock, |
| uint32_t & | endBlock | ||
| ) |
| uint8_t createContiguous | ( | SdFile * | dirFile, |
| const char * | fileName, | ||
| uint32_t | size | ||
| ) |
Create and open a new contiguous file of a specified size.
- Note:
- This function only supports short DOS 8.3 names. See open() for more information.
- Parameters:
-
[in] dirFile The directory where the file will be created. [in] fileName A valid DOS 8.3 file name. [in] size The desired file size.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include fileName contains an invalid DOS 8.3 file name, the FAT volume has not been initialized, a file is already open, the file already exists, the root directory is full or an I/O error.
Definition at line 129 of file SdFile.cpp.
| uint8_t createContiguous | ( | SdFile & | dirFile, |
| const char * | fileName, | ||
| uint32_t | size | ||
| ) |
| uint32_t curCluster | ( | void | ) | const |
| uint32_t curPosition | ( | void | ) | const |
| static void dateTimeCallback | ( | void(*)(uint16_t &date, uint16_t &time) | dateTime ) | [static] |
| static void dateTimeCallback | ( | void(*)(uint16_t *date, uint16_t *time) | dateTime ) | [static] |
Set the date/time callback function.
- Parameters:
-
[in] dateTime The user's call back function. The callback function is of the form:
void dateTime(uint16_t* date, uint16_t* time) { uint16_t year; uint8_t month, day, hour, minute, second; // User gets date and time from GPS or real-time clock here // return date using FAT_DATE macro to format fields *date = FAT_DATE(year, month, day); // return time using FAT_TIME macro to format fields *time = FAT_TIME(hour, minute, second); }
Sets the function that is called when a file is created or when a file's directory entry is modified by sync(). All timestamps, access, creation, and modify, are set when a file is created. sync() maintains the last access date and last modify date/time.
See the timestamp() function.
| static void dateTimeCallbackCancel | ( | void | ) | [static] |
| uint32_t dirBlock | ( | void | ) | const |
| uint8_t dirEntry | ( | dir_t * | dir ) |
Return a files directory entry.
- Parameters:
-
[out] dir Location for return of the files directory entry.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure.
Definition at line 158 of file SdFile.cpp.
| uint8_t dirIndex | ( | void | ) | const |
| void dirName | ( | const dir_t & | dir, |
| char * | name | ||
| ) | [static] |
Format the name field of dir into the 13 byte array name in standard 8.3 short name format.
- Parameters:
-
[in] dir The directory structure containing the name. [out] name A 13 byte char array for the formatted name.
Definition at line 178 of file SdFile.cpp.
| uint32_t fileSize | ( | void | ) | const |
| uint32_t firstCluster | ( | void | ) | const |
| uint8_t isDir | ( | void | ) | const |
| uint8_t isFile | ( | void | ) | const |
| uint8_t isOpen | ( | void | ) | const |
| uint8_t isRoot | ( | void | ) | const |
| uint8_t isSubDir | ( | void | ) | const |
| void ls | ( | uint8_t | flags = 0, |
| uint8_t | indent = 0 |
||
| ) |
List directory contents to Serial.
- Parameters:
-
[in] flags The inclusive OR of
LS_DATE - Print file modification date
LS_SIZE - Print file size.
LS_R - Recursive list of subdirectories.
- Parameters:
-
[in] indent Amount of space before file name. Used for recursive list to indicate subdirectory level.
Definition at line 201 of file SdFile.cpp.
| uint8_t makeDir | ( | SdFile * | dir, |
| const char * | dirName | ||
| ) |
Make a new directory.
- Parameters:
-
[in] dir An open SdFat instance for the directory that will containing the new directory. [in] dirName A valid 8.3 DOS name for the new directory.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include this SdFile is already open, dir is not a directory, dirName is invalid or already exists in dir.
Definition at line 284 of file SdFile.cpp.
| uint8_t open | ( | SdFile & | dirFile, |
| const char * | fileName, | ||
| uint8_t | oflag | ||
| ) |
| uint8_t open | ( | SdFile & | dirFile, |
| uint16_t | index, | ||
| uint8_t | oflag | ||
| ) |
| uint8_t open | ( | SdFile * | dirFile, |
| uint16_t | index, | ||
| uint8_t | oflag | ||
| ) |
Open a file by index.
- Parameters:
-
[in] dirFile An open SdFat instance for the directory. [in] index The index of the directory entry for the file to be opened. The value for index is (directory file position)/32. [in] oflag Values for oflag are constructed by a bitwise-inclusive OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
See open() by fileName for definition of flags and return values.
Definition at line 476 of file SdFile.cpp.
| uint8_t open | ( | SdFile * | dirFile, |
| const char * | fileName, | ||
| uint8_t | oflag | ||
| ) |
Open a file or directory by name.
- Parameters:
-
[in] dirFile An open SdFat instance for the directory containing the file to be opened. [in] fileName A valid 8.3 DOS name for a file to be opened. [in] oflag Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list
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_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), write_P(PGM_P), writeln_P(PGM_P), or the Arduino Print class. These functions do character at a time writes so 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.
- Note:
- Directory files must be opened read only. Write and truncation is not allowed for directory files.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include this SdFile is already open, difFile is not a directory, fileName is invalid, the file does not exist or can't be opened in the access mode specified by oflag.
Definition at line 384 of file SdFile.cpp.
| uint8_t openRoot | ( | SdVolume * | vol ) |
Open a volume's root directory.
- Parameters:
-
[in] vol The FAT volume containing the root directory to be opened.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the FAT volume has not been initialized or it a FAT12 volume.
Definition at line 550 of file SdFile.cpp.
| void printDirName | ( | const dir_t & | dir, |
| uint8_t | width | ||
| ) | [static] |
Print the name field of a directory entry in 8.3 format to Serial.
- Parameters:
-
[in] dir The directory structure containing the name. [in] width Blank fill name if length is less than width.
Definition at line 585 of file SdFile.cpp.
| void printFatDate | ( | uint16_t | fatDate ) | [static] |
Print a directory date field to Serial.
Format is yyyy-mm-dd.
- Parameters:
-
[in] fatDate The date field from a directory entry.
Definition at line 612 of file SdFile.cpp.
| void printFatTime | ( | uint16_t | fatTime ) | [static] |
Print a directory time field to Serial.
Format is hh:mm:ss.
- Parameters:
-
[in] fatTime The time field from a directory entry.
Definition at line 626 of file SdFile.cpp.
| void printTwoDigits | ( | uint8_t | v ) | [static] |
Print a value as two digits to Serial.
- Parameters:
-
[in] v Value to be printed, 0 <= v <= 99
Definition at line 638 of file SdFile.cpp.
| int16_t read | ( | void | ) |
| int16_t read | ( | void * | buf, |
| uint16_t | nbyte | ||
| ) |
Read data from a file starting at the current position.
- Parameters:
-
[out] buf Pointer to the location that will receive the data. [in] nbyte Maximum number of bytes to read.
- Returns:
- For success read() returns the number of bytes read. A value less than nbyte, including zero, will be returned if end of file is reached. If an error occurs, read() returns -1. Possible errors include read() called before a file has been opened, corrupt file system or an I/O error occurred.
Definition at line 660 of file SdFile.cpp.
| int8_t readDir | ( | dir_t * | dir ) |
Read the next directory entry from a directory file.
- Parameters:
-
[out] dir The dir_t struct that will receive the data.
- Returns:
- For success readDir() returns the number of bytes read. A value of zero will be returned if end of file is reached. If an error occurs, readDir() returns -1. Possible errors include readDir() called before a directory has been opened, this is not a directory file or an I/O error occurred.
Definition at line 724 of file SdFile.cpp.
| static uint8_t remove | ( | SdFile & | dirFile, |
| const char * | fileName | ||
| ) | [static] |
| uint8_t remove | ( | SdFile * | dirFile, |
| const char * | fileName | ||
| ) | [static] |
Remove a file.
The directory entry and all data for the file are deleted.
- Parameters:
-
[in] dirFile The directory that contains the file. [in] fileName The name of the file to be removed.
- Note:
- This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file is a directory, is read only, dirFile is not a directory, fileName is not found or an I/O error occurred.
Definition at line 810 of file SdFile.cpp.
| uint8_t remove | ( | void | ) |
Remove a file.
The directory entry and all data for the file are deleted.
- Note:
- This function should not be used to delete the 8.3 version of a file that has a long name. For example if a file has the long name "New Text Document.txt" you should not delete the 8.3 name "NEWTEX~1.TXT".
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file read-only, is a directory, or an I/O error occurred.
Definition at line 774 of file SdFile.cpp.
| void rewind | ( | void | ) |
| uint8_t rmDir | ( | void | ) |
Remove a directory file.
The directory file will be removed only if it is empty and is not the root directory. rmDir() follows DOS and Windows and ignores the read-only attribute for the directory.
- Note:
- This function should not be used to delete the 8.3 version of a directory that has a long name. For example if a directory has the long name "New folder" you should not delete the 8.3 name "NEWFOL~1".
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include the file is not a directory, is the root directory, is not empty, or an I/O error occurred.
Definition at line 831 of file SdFile.cpp.
| uint8_t rmRfStar | ( | void | ) |
Recursively delete a directory and all contained files.
This is like the Unix/Linux 'rm -rf *' if called with the root directory hence the name.
Warning - This will remove all contents of the directory including subdirectories. The directory will then be removed if it is not root. The read-only attribute for files will be ignored.
- Note:
- This function should not be used to delete the 8.3 version of a directory that has a long name. See remove() and rmDir().
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure.
Definition at line 869 of file SdFile.cpp.
| uint8_t seekCur | ( | uint32_t | pos ) |
| uint8_t seekEnd | ( | void | ) |
| uint8_t seekSet | ( | uint32_t | pos ) |
Sets a file's position.
- Parameters:
-
[in] pos The new position in bytes from the beginning of the file.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure.
Definition at line 916 of file SdFile.cpp.
| void setUnbufferedRead | ( | void | ) |
Use unbuffered reads to access this file.
Used with Wave Shield ISR. Used with Sd2Card::partialBlockRead() in WaveRP.
Not recommended for normal applications.
| uint8_t sync | ( | void | ) |
The sync() call causes all modified data and directory fields to be written to the storage device.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include a call to sync() before a file has been opened or an I/O error.
Definition at line 957 of file SdFile.cpp.
| uint8_t timestamp | ( | uint8_t | flags, |
| uint16_t | year, | ||
| uint8_t | month, | ||
| uint8_t | day, | ||
| uint8_t | hour, | ||
| uint8_t | minute, | ||
| uint8_t | second | ||
| ) |
Set a file's timestamps in its directory entry.
- Parameters:
-
[in] flags Values for flags are constructed by a bitwise-inclusive OR of flags from the following list
T_ACCESS - Set the file's last access date.
T_CREATE - Set the file's creation date and time.
T_WRITE - Set the file's last write/modification date and time.
- Parameters:
-
[in] year Valid range 1980 - 2107 inclusive. [in] month Valid range 1 - 12 inclusive. [in] day Valid range 1 - 31 inclusive. [in] hour Valid range 0 - 23 inclusive. [in] minute Valid range 0 - 59 inclusive. [in] second Valid range 0 - 59 inclusive
- Note:
- It is possible to set an invalid date since there is no check for the number of days in a month.
- Modify and access timestamps may be overwritten if a date time callback function has been set by dateTimeCallback().
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure.
Definition at line 1017 of file SdFile.cpp.
| uint8_t truncate | ( | uint32_t | length ) |
Truncate a file to a specified length.
The current file position will be maintained if it is less than or equal to length otherwise it will be set to end of file.
- Parameters:
-
[in] length The desired length for the file.
- Returns:
- The value one, true, is returned for success and the value zero, false, is returned for failure. Reasons for failure include file is read only, file is a directory, length is greater than the current file size or an I/O error occurs.
Definition at line 1065 of file SdFile.cpp.
| uint8_t type | ( | void | ) | const |
| uint8_t unbufferedRead | ( | void | ) | const |
| SdVolume* volume | ( | void | ) | const |
| size_t write | ( | const void * | buf, |
| uint16_t | nbyte | ||
| ) |
Write data to an open file.
- Note:
- Data is moved to the cache but may not be written to the storage device until sync() is called.
- Parameters:
-
[in] buf Pointer to the location of the data to be written. [in] nbyte Number of bytes to write.
- Returns:
- For success write() returns the number of bytes written, always nbyte. If an error occurs, write() returns -1. Possible errors include write() is called before a file has been opened, write is called for a read-only file, device is full, a corrupt file system or an I/O error.
Definition at line 1124 of file SdFile.cpp.
| size_t write | ( | uint8_t | b ) |
Write a byte to a file.
Required by the Arduino Print class.
Use SdFile::writeError to check for errors.
Definition at line 1223 of file SdFile.cpp.
| size_t write | ( | const char * | str ) |
Write a string to a file.
Used by the Arduino Print class.
Use SdFile::writeError to check for errors.
Definition at line 1232 of file SdFile.cpp.
| void write_P | ( | PGM_P | str ) |
Write a PROGMEM string to a file.
Use SdFile::writeError to check for errors.
Definition at line 1241 of file SdFile.cpp.
| void writeln_P | ( | PGM_P | str ) |
Write a PROGMEM string followed by CR/LF to a file.
Use SdFile::writeError to check for errors.
Definition at line 1250 of file SdFile.cpp.
Generated on Thu Jul 14 2022 02:07:54 by
1.7.2