Problem with SD library

Hello everybody,

After having lasted many hours on my application, I think that the problem comes from the SD library. That is :

if I first want to list files then read/write files, that’s OK. But, if I first read/write files and after I want to list files, the second listing is wrong (missing files).

To show you the problem, I have modified the example “listfile” in which I have added a write file between two listing. The first listing is correct, the write is correct but the second listing is wrong (missing files).

For the test, I have a Mega2560 board with TouchScreen 320 x 240 that supports the SD card reader. I use a
2 Mb SD card on which I have some files at the root…

Did you meet this problem ? Can you test the included sketch and tell me what are the results you obtain.

NOTE, be carefull to the right value to set in : “SD.begin(xx)” ( xx = 53 is for my case)

Thank you for your help.


  SD card basic file example
 This example shows how to create and destroy 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
 modified 9 Apr 2012
 by Tom Igoe
 This example code is in the public domain.
#include <SPI.h>
#include <SD.h>

File myFile;
File root;

void setup()
  // Open serial communications and wait for port to open:
  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(53)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");

  root ="/");

  printDirectory(root, 0);



  root ="/");

  printDirectory(root, 0);


void loop()
  // nothing happens after setup finishes.

void printDirectory(File dir, int numTabs) {
  while(true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
    for (uint8_t i=0; i<numTabs; i++) {
    if (entry.isDirectory()) {
      printDirectory(entry, numTabs+1);
    else {
      // files have sizes, directories do not
      Serial.println(entry.size(), DEC);

void ecritFichier() {

  myFile ="test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    Serial.println("testing 1, 2, 3.");
    // close the file:

You should post a link to the Touchscreen hardware and a description on how it is connected to the Mega.

You should have posted this in the "Storage" section of the forum.


my set is as follow :

Sorry, I thought that troubleshooting was the right section. How to move to the right sction ?



I'll see if I can get a moderator to move your thread to the Storage section. You will probably get more help there than here.

I don't know how the SD library handles more than one file open at a time. You seem to have two open at once.

Thank you SurferTim,

In the SD.h, it said that “only one file can be open at a time.” It’s what I pay attention to do. But about Handle, nothing is said. So how to release a Handle ?


root in your setup is a file handle.


You also try to open a file in ecritFichier(); It's file handle is myFile.

You are right.

In the sketch, I have deleted "myFile" and replaced it by "root", so I have just one handle.

I also have added "root.close();" after the first call to printDirectory(root, 0);"

the problem is always the same ???