Class SdFatFile
Description
Defines a class of SD FAT File.
Syntax
class SdFatFile
Members
Public Constructors |
|
---|---|
SdFatFile::SdFatFile |
Constructs a SdFatFile object |
SdFatFile::~SdFatFile |
Destructs a SdFatFile object |
Public Methods |
|
SdFatFile::write |
Write 1 byte/bytes to file |
SdFatFile::read |
Read 1 byte/bytes from the file |
SdFatFile::peek |
Read 1 byte from file without move curser |
SdFatFile::available |
Check if the cursor is at EOF (End-Of-File) |
SdFatFile::bool |
Check if file is opened |
SdFatFile::seek |
Change cursor to a specific position |
SdFatFile::close |
Close file |
- SdFatFile::write
Description
Write 1 byte or bytes to the file.
Syntax
size_t SdFatFile::write(uint8_t c);
size_t SdFatFile::write(const uint8_t *buf, size_t size);
Parameters
c
: The character to be written.
buf
: The buffer to be written.
size
: The length of buffer to be written.
Returns
The function returns the number of byte count that has been successfully written to the file.
Example Code
NA.
Notes and Warnings
Include “SdFatFile.h” to use the class function.
- SdFatFile:: read
Description
Read 1 byte or bytes from the file.
Syntax
int SdFatFile::read(void);
int SdFatFile::read(void *buf, uint16_t nbyte);
Parameters
buf
: The buffer to store the content.
nbyte
: The buffer size. (Or can be regarded as the desired length to read).
Returns
The function returns a read character or the read size of the buffer.
Example Code
#include 「FatFs_SD.h」
char dirname[] = 「testdir」;
char filename[] = 「test.txt」;
char write_content[] = 「hello world!」;
FatFsSD fs;
void setup() {
char buf[128];
char absolute_filename[128];
fs.begin();
sprintf(absolute_filename, 「%s%s」, fs.getRootPath(), dirname);
fs.mkdir(absolute_filename);
printf(「create dir at \」%s"rn」, absolute_filename);
sprintf(absolute_filename, 「%s%s/%s」, fs.getRootPath(), dirname, filename);
SdFatFile file = fs.open(absolute_filename);
file.println(write_content);
file.close();
printf(「create file at \」%s"rn」, absolute_filename);
printf(「read back from \」%s"rn」, absolute_filename);
file = fs.open(absolute_filename);
memset(buf, 0, sizeof(buf));
file.read(buf, sizeof(buf));
file.close();
printf(「==== content ====rn」);
printf(「%s」, buf);
printf(「==== end ====rn」);
fs.end();
}
void loop() {
delay(1000);
}
#include 「FatFs_SD.h」
char filename[] = 「test.txt」;
char write_content[] = 「hello world!」;
FatFsSD fs;
void setup() {
char buf[128];
char absolute_filename[128];
fs.begin();
printf(「write something to \」%s"rn」, filename);
sprintf(absolute_filename, 「%s%s」, fs.getRootPath(), filename);
SdFatFile file = fs.open(absolute_filename);
file.println(write_content);
file.close();
printf(「write finishrnrn」);
printf(「read back from \」%s"rn」, filename);
file = fs.open(absolute_filename);
memset(buf, 0, sizeof(buf));
file.read(buf, sizeof(buf));
file.close();
printf(「==== content ====rn」);
printf(「%s」, buf);
printf(「==== end ====rn」);
fs.end();
}
void loop() {
delay(1000);
}
Notes and Warnings
Include “SdFatFile.h” to use the class function.
- SdFatFile:: peek
Description
Read one byte from the file without moving the curser.
Syntax
int SdFatFile::peek(void);
Parameters
The function requires no input parameter.
Returns
The function returns the read character as an integer number.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” to use the class function.
- SdFatFile:: available
Description
Check if the cursor is at EOF.
Syntax
int SdFatFile::available(void);
Parameters
The function requires no input parameter.
Returns
The function returns 0
if the cursor is at EOF, else returns “1”.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” to use the class function.
- SdFatFile:: flush
Description
It is a nop. This is an inherited function from class Stream. And it does not affect SD File.
Syntax
void SdFatFile::flush(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” to use the class function.
- SdFatFile:: seek
Description
Change cursor to a specific position.
Syntax
int SdFatFile::seek(uint32_t pos);
Parameters
pos: The desired position.
Returns
The function returns 0 if success otherwise returns a negative value.
Example Code
NA
Notes and Warnings
Include “SdFatFile.h” in order to use the class function.
- SdFatFile:: close
Description
Close file.
Syntax
int SdFatFile::close(void);
Parameters
The function requires no input parameter.
Returns
The function returns 0 if runs successfully otherwise it returns a negative value.
Example Code
Example: last_modified_time;
The example shows how to get and set last modified time of a file.
Example: create_folder;
This example shows how to create a folder and open a file under it.
the details of the code can be found in the section of SdFatFile::read.
Example: file_read_write;
This example shows how to open/close files and perform read/write to
it. The details of the code can be found in the section of SdFatFile::read.
#include <FatFs_SD.h>
FatFsSD fs;
char filename[] = 「test.txt」;
void setup() {
char absolute_filename[128];
uint16_t year = 2021;
uint16_t month = 4;
uint16_t date = 4;
uint16_t hour = 12;
uint16_t minute = 12;
uint16_t second = 12;
fs.begin();
sprintf(absolute_filename, 「%s%s」, fs.getRootPath(), filename);
SdFatFile file = fs.open(absolute_filename);
file.close();
fs.setLastModTime(absolute_filename, year, month, date, hour, minute, second);
fs.getLastModTime(absolute_filename, &year, &month, &date, &hour, &minute, &second);
printf(「filename:"%s"rn」, absolute_filename);
printf(「time mod:%04d/%02d/%02d %02d:%02d:%02drn」, year, month, date, hour, minute, second);
fs.end();
}
void loop() {
delay(1000);
}
Notes and Warnings
Include “SdFatFile.h” in order to use the class function.