Hardware Reference
In-Depth Information
The File object is created when opening the i le. This function takes no
parameters and does not return any data.
File myFile;
myFile = SD.open("data.dat", FILE_WRITE);
// Perform any read or write operations here
myFile.close();
Reading and Writing Files
Reading i les is done with a pointer to a i le position. By default, when a i le is
opened, this pointer is set to the beginning of the i le (byte 0). As each byte is
read in, the pointer increments, until it reaches the end of the i le. You can set
the position of the pointer to any location inside the i le.
Writing i les is done by either appending data to the end of the i le, no matter
where the pointer is located, or writing data at the i le pointer location.
When reading and writing to a i le, you will be using the File class, which
inherits from Stream, just like Serial does.
Reading Files
To read a byte from a i le, use the read() function of the File class.
data = file.read();
This function returns 1 byte at a time (or −1 if no data is available) and auto-
matically updates the pointer. If you do not want the pointer to be updated,
you can call peek() .
data = file.peek();
Its use is exactly the same as read() , returning 1 byte, but the pointer is not
updated. Several calls to peek() returns the same byte. To know the value of
the pointer (to know which byte is the next to be read), use position() .
result = file.position();
This function does not take any parameters and returns an unsigned long
indicating the current position within the i le. It is also possible to set the posi-
tion with seek() .
result = file.seek(position);
This function attempts to set the i le pointer to the value of position , dei ned
as an unsigned long . To know the size of the current open i le, use size() . It
returns the i le size in bytes as an unsigned long .
data = file.size();
 
Search WWH ::




Custom Search