Go Down

Topic: Sd.exists doesn't find the file (Read 395 times) previous topic - next topic



I have a uSD card which has been formatted to FAT16, I have made a file on this card called "file.txt". I can successfully open and read the contents of the file. However when I want to remove the file and I use the SD.exists("file.txt") it doesn't seem to be able to find the file at all!

Any ideas?

Code as follows:

Code: [Select]

#include <SPI.h>
#include <SD.h>

File myFile;

void setup()



void RemoveFile()

        Serial.println("file removed");
        Serial.println("no file to remove");


void ReadFileContents()

  // re-open the file for reading:
  myFile = SD.open("file.txt");
  if (myFile) {

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
    // close the file:
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening file.txt");



I suspect your Arduino removes the file immediately after you upload the sketch to it, and then when you open the Serial monitor the Arduino resets and tries to remove the file again, which fails because it's already been removed.

Go Up