SD Library File Read Write Edit

I really fade-up about SD library Version behavior.

i check same coding for Uno, Mega, ESP32
(i only change some required port number depend by hardware).

SD library different Version different different behavior about
seek(), FILE_APPEND, FILE_WRITE

Also SD library latest Version 1.2.4 not work properly.

I am using Arduino IDE & PlatformIO IDE, SD library not work properly.

Any body have any solution ?

May be replace other any library for Micro SD card handling.
Or Any solution...............

Not true. The library works.

Show your code here using the < CODE > button in the message box.

Also, consider using "translate.google.com" to write in your natural language and copy/paste the translation to English.

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky notes in https://forum.arduino.cc/c/using-arduino/uncategorized/184.


You'll have to show a your code (just in case, don't forget to use code tags as described in How to get the best out of this forum). You will also have to describe the different behaviours.

Please elaborate what that means.

I would expect esp32 to be different but uno and mega should be the same..
I remember something with seeking and writing when I was coding for an Uno..

//random write mode
#define FILE_RANDOM_WRITE (O_WRITE | O_READ | O_CREAT)

seemed to work for me..
i was seeking/writing on a file of structures..
Uno SD Db

good luck.. ~q

1 Like

For Uno

#include <SPI.h>
#include <SD.h>
#define pincs = 10; // For Uno
File myFile;

void setup() {
  
  Serial.begin(9600);
  delay(3000);
  while (!Serial) 
        { ; }  // wait for serial port to connect. 
  Serial.println("Initializing Serial Port.");

  if (!SD.begin()) 
      { Serial.println("SD Card initialization failed!");
        while (1); 
      }
  Serial.println("SD Card initialization done.");

//File Writing
   myFile = SD.open("/test.txt", FILE_WRITE);
  if (myFile) 
         {
             Serial.println("File writing from 0 byte.");
             myFile.seek(0);                  //Write from 0 byte 
             myFile.println("20240111");
             myFile.println("20240112");
             myFile.println("20240113");
 
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File write done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())   // read from the file until there's nothing else in it:
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line 
              
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");


//File Appending
   myFile = SD.open("/test.txt",O_APPEND); //For Uno
  if (myFile) 
         {
             Serial.println("File Append Start from 0 byte");
             myFile.seek(0);
             myFile.println("20240114");
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File Append  done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())  
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line 
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");

// File Edit:
   myFile = SD.open("/test.txt", FILE_WRITE);
  if (myFile) 
         {
             Serial.println("File Edit Start from 0 byte");
             myFile.seek(0);
             myFile.println("20240120");
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File Edit done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())   // read from the file until there's nothing else in it:
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line 
               
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");


}

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

Result is:
Initializing Serial Port.
SD Card initialization done.
File writing from 0 byte.
File write done.
File Read from 0 byte:
Position: 0 --> 20240111
Position: 10 --> 20240112
Position: 20 --> 20240113
File Reading done.
File Append Start from 0 byte
File Append done.
File Read from 0 byte:
Position: 0 --> 20240111
Position: 10 --> 20240112
Position: 20 --> 20240113
File Reading done.
File Edit Start from 0 byte
File Edit done.
File Read from 0 byte:
Position: 0 --> 20240111
Position: 10 --> 20240112
Position: 20 --> 20240113
Position: 30 --> 20240120
File Reading done.

For ESP32

#include <Arduino.h>   //For ESP32
#include <SPI.h>
#include <SD.h>
#define pincs = 5; // For ESP32
File myFile;

void setup() {
  
  Serial.begin(9600);
  delay(3000);
  while (!Serial) 
        { ; }  // wait for serial port to connect. 
  Serial.println("Initializing Serial Port.");

  if (!SD.begin()) 
      { Serial.println("SD Card initialization failed!");
        while (1); 
      }
  Serial.println("SD Card initialization done.");

//File Writing
   myFile = SD.open("/test.txt", FILE_WRITE);
  if (myFile) 
         {
             Serial.println("File writing from 0 byte.");
             myFile.seek(0);                  //Write from 0 byte 
             myFile.println("20240111");
             myFile.println("20240112");
             myFile.println("20240113");
 
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File write done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())   // read from the file until there's nothing else in it:
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line 
              
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");


//File Appending
   myFile = SD.open("/test.txt",FILE_APPEND); //For ESP32
  if (myFile) 
         {
             Serial.println("File Append Start from 0 byte");
             myFile.seek(0);
             myFile.println("20240114");
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File Append  done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())  
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line 
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");

// File Edit:
   myFile = SD.open("/test.txt", FILE_WRITE);
  if (myFile) 
         {
             Serial.println("File Edit Start from 0 byte");
             myFile.seek(0);
             myFile.println("20240120");
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File Edit done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())   // read from the file until there's nothing else in it:
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line 
               
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");


}

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

Result is :Initializing Serial Port.
SD Card initialization done.
File writing from 0 byte.
File write done.
File Read from 0 byte:
Position: 0 --> 20240111
Position: 10 --> 20240112
Position: 20 --> 20240113
File Reading done.
File Append Start from 0 byte
File Append done.
File Read from 0 byte:
Position: 0 --> 20240111
Position: 10 --> 20240112
Position: 20 --> 20240113
Position: 30 --> 20240114
File Reading done.
File Edit Start from 0 byte
File Edit done.
File Read from 0 byte:
Position: 0 --> 20240120

File Reading done.

Now i shared Uno & ESP32 code
I use
Arduino IDE Version: 2.3.2
SD Library Version: 1.2.4

Please suggest me.....

I don't believe you will be able to make this work..
At least not on a text file using println..

Appending to a randomly seeked position won't work and you should seek to the end in order to add records..

Now you can seek to a record position and edit the position..

If you really must append randomly, then another temp file would have to be used..

I'm kind of used to this behavior and know that I should treat a text file and a type file, differently..

When I get a chance today, I will try to port the sketch I posted to esp32, don't see why it wouldn't..

Maybe you could switch to a type file instead of text file..

good luck.. ~q

As your previous suggestion
i change code for Arduino Uno & Mega Result is good.

For Uno


#include <SPI.h>
#include <SD.h>
#define FILE_RANDOM_WRITE (O_WRITE | O_READ | O_CREAT)

#define pincs = 10; // For Uno
File myFile;

void setup() {
  
  Serial.begin(9600);
  delay(3000);
  while (!Serial) 
        { ; }  // wait for serial port to connect. 
  Serial.println("Initializing Serial Port.");

  if (!SD.begin()) 
      { Serial.println("SD Card initialization failed!");
        while (1); 
      }
  Serial.println("SD Card initialization done.");

//File Writing
 //  myFile = SD.open("/test.txt", FILE_WRITE);
   myFile = SD.open("/test.txt", FILE_RANDOM_WRITE);
  if (myFile) 
         {
             Serial.println("File writing from 0 byte.");
             myFile.seek(0);                  //Write from 0 byte 
             myFile.println("20240111");
             myFile.println("20240112");
             myFile.println("20240113");
 
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File write done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())   // read from the file until there's nothing else in it:
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line Header Line
              
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");


//File Appending
   myFile = SD.open("/test.txt",FILE_RANDOM_WRITE);
  if (myFile) 
         {
             Serial.println("File Append Start from 0 byte");
             myFile.seek(0);
             myFile.println("20240114");
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File Append  done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())  
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line Header Line
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");

// File Edit:
   myFile = SD.open("/test.txt", FILE_RANDOM_WRITE);
  if (myFile) 
         {
             Serial.println("File Edit Start from 0 byte");
             myFile.seek(0);
             myFile.println("20240120");
          }
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:                                         
   myFile.close();  // close the file:
   Serial.println("File Edit done.");


   // File Reading:
   myFile = SD.open("/test.txt");
   if (myFile) 
         {
        Serial.println("File Read from 0 byte:"); 
        myFile.seek(0);  
        while (myFile.available())   // read from the file until there's nothing else in it:
              {
                Serial.print(" Position: "); Serial.print(myFile.position()); Serial.print(" --> ");
                Serial.println(myFile.readStringUntil('\n')); //until new line Header Line
               
               } 
          } 
   else   { Serial.println("Error Opening File"); }  // if the file didn't open, print an error:      
   myFile.close(); // close the file:    
   Serial.println("File Reading done.");


}

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

Result is:
Initializing Serial Port.
SD Card initialization done.
File writing from 0 byte.
File write done.
File Read from 0 byte:
Position: 0 --> 20240111
Position: 10 --> 20240112
Position: 20 --> 20240113
File Reading done.
File Append Start from 0 byte
File Append done.
File Read from 0 byte:
Position: 0 --> 20240114
Position: 10 --> 20240112
Position: 20 --> 20240113
File Reading done.
File Edit Start from 0 byte
File Edit done.
File Read from 0 byte:
Position: 0 --> 20240120
Position: 10 --> 20240112
Position: 20 --> 20240113
File Reading done.

Now working Fine in Arduino Uno.
But i need also ESP32.

And you ok with the result, with what you call Append??
Append mean to add too..
Edit the position is more like what you have done..
If you append had worked, i would have expected..
14
11
12
13
instead you edited the data from 11 to 14..
is this behavior ok??

~q

Yes, you are correct.
APPEND not work.
Any good way about Append.

you already write one way.but two file need more delay time.

No, sadly, there's isn't, the whole file needs to be rewritten..
how many records/lines do you expect??
how fast will the file be written??
maybe there's a different way..
~q

The ESP32 SD libraries are different to other platforms.

I suspect that was a choice that Espressif made, not sure why, maybe ask them if they could make their SD library 'compatible' ?

50 Line for 2 Second .

Maybe use a vector..
Store and sort in memory, then save it to the SD once..

here's an example of using a vector and saving to preferences..
if your data really is numeric, don't save it as a string in the vector, will be harder to sort..
could use unsigned long or long, when you actually write the file you can use strings if you plan on examining the contents with eyes..

good luck.. ~q

1 Like

Sir Thank you for suggestion.

lastly i need ESP32 with SD card same as Uno work.
What is the library to handle SD card with ESP32 ?

give me your suggestion for ESP32.

SD.h

I already share my coding for ESP32 & face problem .Do you have any solution ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.