Arduino SD.open()
Description
The SD.open() function opens a file on the SD card. In the case the file is opened for writing, a new file will be created if it doesn't already exist (but the directory containing it must already exist).
Syntax
SD.open(filename)
SD.open(filename, mode)
Parameters
- filename: the name the file to open, which can include directories (delimited by forward slashes, /). Allowed type: char * or String
- mode: (optional) the mode in which to open the file. If it is not set, the defaults value is FILE_READ. The mode can be one of the following values:
- FILE_READ: open the file for reading, starting at the beginning of the file.
- FILE_WRITE: open the file for reading and writing, starting at the end of the file.
- The filename should follows 8.3 filename convention. If not, it may fails to open.
- If a file is not closed before openning it again, it may fails to open.
※ NOTES AND WARNINGS:
Returns
- A File object referring to the opened file. If the file couldn't be opened, this object will evaluate to false in a boolean context. For example, you can test the return value with if statement.
Example Code
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/reference/library/arduino-sd.open
*/
#include <SD.h>
#define PIN_SPI_CS 4
File file;
void setup() {
Serial.begin(9600);
if (!SD.begin(PIN_SPI_CS)) {
Serial.println("SD CARD FAILED, OR NOT PRESENT!");
while (1); // don't do anything more:
}
file = SD.open("arduino.txt", FILE_READ);
if (file) {
Serial.println("SD Card: file is opened");
file.close();
} else {
Serial.println("SD Card: file is failed to open");
}
}
void loop() {
}
Tutorials
See Also
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .
Additionally, some links direct to products from our own brand, DIYables .