Pages: [1]   Go Down
Author Topic: SD CARD Create File and then Read Directory Problem  (Read 2805 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello All,

I have stumbled upon a problem that I can't get past and need you assistance. I have been reading everything I could search for and have not found a solution although I am fairly certain it is something trivial.

On setup I am creating a file on the SD card if it doesn't exist which is working fine. Later on I want to get a list of file in the main directory. The issue is that when I create a file in setup and then later try to get a list of the files the files will not list. However, if I comment out the portion in Setup that creates the file I can get a list of files without a problem.

I have modified the listfiles example below to illustrate my point. What am I doing wrong. It's almost like even though I am closing the SD open file it is still open.

Code:
#include <SD.h>

File root;
File dataFile;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  dataFile = SD.open("TEST.CSV", O_CREAT | O_APPEND | O_WRITE | O_READ); //if I comment this out then the list files works
  dataFile.close();
   Serial.println("Created File: TEST.CSV");

}

void loop()
{
  dataFile = SD.open("/");
  
  printDirectory(dataFile, 0);
  
  Serial.println("done!");  
  
  delay(1000);
  
  dataFile.close();
    
}

void printDirectory(File dir, int numTabs) {
   while(true) {
    
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
           entry.close();
   }


Any help would be very appreciated.

Thanks!
« Last Edit: November 11, 2012, 04:37:54 pm by brian2sos » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this.
Code:
dataFile = SD.open("TEST.CSV", FILE_WRITE);

Use this to "truncate"...
Code:
SD.remove("TEST.CSV");
... then open for writing and it is empty.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks I'll give that a shot
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use this to locate files and try to remove stuff which you don't need

Code:
File dataFile;
Sd2Card card;
SdVolume volume;
SdFile root;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  pinMode(10, OUTPUT);
 
  if (!card.init(SPI_HALF_SPEED, 4)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present.");
  }
 
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  dataFile = SD.open("examlpe.CSV", FILE_WRITE); //if I comment this out then the list files works
  if (dataFile) {
    Serial.print(F("Writing to test.txt..."));
    dataFile.println("NOW this FILE CREATED");
    dataFile.close();
    Serial.println(F("done."));
  }
 
 
 
   Serial.println("Created File: examlpe.CSV");
     
  // re-open the file for reading:
  dataFile = SD.open("examlpe.CSV");
  if (dataFile) {
    Serial.println("examlpe.CSV:");
   
    // read from the file until there's nothing else in it:
    while (dataFile.available()) {
    Serial.write(dataFile.read());
    }
    // close the file:
    dataFile.close();
  } else {
  // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}


void loop()
{
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}

cheers
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this.
Code:
dataFile = SD.open("TEST.CSV", FILE_WRITE);

Use this to "truncate"...
Code:
SD.remove("TEST.CSV");
... then open for writing and it is empty.


Hey, what do you mean by "truncate" the file?

Does the file need to be "open" in order for "remove" to work?

More generally, what does "SD.remove(filename)" actually do?  I'm having problems where I'll do something like
Code:
if(!SD.remove(filename))
{
    Serial.println("File removal failed");
}
else
{
    Serial.println("File successfully removed");
}

and it prints the "Success" message to the serial monitor, which leads me to believe that the file was removed.  However, when I run the "listfiles" sketch from the Arduino SD Library Examples, it says that the file I tried to delete is there with some filesize.

Does SD.remove actually remove the file, and all its contents from the SD card?  This may seem like a dumb question, but I've had a reasonable amount of success with the other SD functions.  Remove is just not working like I expect it to.

Thanks.
Logged

Pages: [1]   Go Up
Jump to: