Adding SD.remove(file) and another example.

This commit is contained in:
David A. Mellis 2010-11-20 13:07:59 -05:00
parent 5af5619df4
commit 6f0ea10600
3 changed files with 53 additions and 4 deletions

View File

@ -294,10 +294,9 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
*/
if (isLastComponent) {
SDClass *p_MemoryCard = static_cast<SDClass*>(object);
p_MemoryCard->file.open(parentDir, filePathComponent,
p_MemoryCard->fileOpenMode);
p_MemoryCard->c = -1;
SDClass *p_SD = static_cast<SDClass*>(object);
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
p_SD->c = -1;
// TODO: Return file open result?
return false;
}
@ -305,6 +304,16 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
}
boolean callback_remove(SdFile& parentDir, char *filePathComponent,
boolean isLastComponent, void *object) {
if (isLastComponent) {
SdFile::remove(parentDir, filePathComponent);
return false;
}
return true;
}
/* Implementation of class used to create `SDCard` object. */
@ -415,4 +424,8 @@ boolean SDClass::mkdir(char *filepath) {
return walkPath(filepath, root, callback_makeDirPath);
}
void SDClass::remove(char *filepath) {
walkPath(filepath, root, callback_remove);
}
SDClass SD;

View File

@ -60,6 +60,9 @@ public:
// Create the requested directory heirarchy--if intermediate directories
// do not exist they will be created.
boolean mkdir(char *filepath);
// Delete the file.
void remove(char *filepath);
private:
SdFile file;

View File

@ -0,0 +1,33 @@
#include <SD.h>
File f;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
SD.begin();
Serial.println("done.");
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
else Serial.println("example.txt doesn't exist.");
Serial.println("Creating example.txt...");
f = SD.open("example.txt", true);
f.close();
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
else Serial.println("example.txt doesn't exist.");
Serial.println("Removing example.txt...");
SD.remove("example.txt");
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
else Serial.println("example.txt doesn't exist.");
}
void loop()
{
}