Pages: [1]   Go Down
Author Topic: Remove file with "SdFat.h"  (Read 4667 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
i need to remove files from Sd card after i red it.
I'm using SdFat.h.
This is "ReadWriteSdFat" example with the last one code line added by me:
Code:
// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;
/*
 SD card read/write
 
 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 modified by Bill Greiman 11 Apr 2011
 This example code is in the public domain.
 
 */
#include <SdFat.h>
SdFat sd;
SdFile myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem
 
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  Serial.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");

  // close the file:
  myFile.close();
  Serial.println("done.");

  // re-open the file for reading:
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
  Serial.println("test.txt:");

  // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) Serial.write(data);
  // close the file:
  if (!myFile.close()) Serial.println("Error file.close");
  if (!myFile.remove()) Serial.println("Error file.remove");
}

void loop() {
  // nothing happens after setup
}


But i get "file remove" error.
« Last Edit: July 12, 2013, 07:58:10 am by rents85 » Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You must not close the file before calling remove.

Just use remove instead of close like this:
Code:
// read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) Serial.write(data);
  // remove the file:
  if (!myFile.remove()) Serial.println("Error file.remove");
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now it works.
Thank you.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi fat16lib,

I wonder if you can help me please?

Code:
 SdFile delfile;
  if (!delfile.open(infile, O_READ)) {
    sd.errorHalt("opening infile for read failed");
  }
  if (!delfile.remove()) {
    sd.errorHalt("deleting infile failed");
  }
 

Syntaxicly, this is apparently correct, but I always get deleting infile failed....   the only thing I can think is that the reason is because:-

Code:
infile = "ROUTE/XXXXXXXX.CSV";

Could you confirm if this is the reason please? If so, how would I achieve the result I am after?

EDIT: That is NOT the problem.....I put the source files in 'root' same result.... Baffled now...

Regards,

Graham
« Last Edit: September 14, 2013, 01:47:28 pm by ghlawrence2000 » Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the problem is that the file is opened read only.
Code:
  if (!delfile.open(infile, O_READ)) {

Try open with write.
Code:
  if (!delfile.open(infile, O_READ | O_WRITE)) {
Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1604
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can just call
Code:
sd.remove(infile);
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 89
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can just call
Code:
sd.remove(infile);

Perfect, thankyou.

Graham
Logged

Pages: [1]   Go Up
Jump to: