Go Down

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

matinzk

Hi,

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()
{
   Serial.begin(9600);

   //ReadFileContents();
   RemoveFile();

}


void RemoveFile()
{

     if(SD.exists("file.txt"))
      {
        SD.remove("file.txt");
        Serial.println("file removed");
      }
      else
      {
        Serial.println("no file to remove");
      }


}

void ReadFileContents()
{

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

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

}


christop

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