Read Line by Line SD Card

Hi, I'm in little trouble, I need to be able to read a line of text from a .txt file on an SD card. I've been searching the forums and I only found like two people with this problem, but I think that only one managed to get close to the answer, is this:

Read a specific line from SD card

I don't know if it succeeded, but with trying to use the codes they sent in that forum and they didn't work for me.

Ok, here are the specifications you always ask for:

  • I want to be able to read any line of a file just by changing the value of a variable (a line can be identified as '\n' (enter) or a comma, whichever is best for me to be able to identify a line).
  • I am using the SD.h library with the latest version.

If it wasn't too much trouble, I'd like an example of a functional code...

Thank you!!!

No magic bullet there...

just read character by character until you find the Nth separator and the data between the (N-1)th and Nth one is your ‘line’

Yes, that's the logic they explained in the other forum, but I tried to replicate it and it doesn't work...

Here you go, with "myFile.seek (3 * 5);" you can search for anything, 3 represents the row, 5 is the width of each row, this is very easy if all the lines have the same width, but if you know the number of byts that there are from the beginning to your destination, well there will not be any problem. When Entering the file, going from one line to another takes 2 bytes.

Code:

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
Serial.begin(9600);
while (!Serial) {
;
}

Serial.println("Iniciando tarjeta SD...");

if (!SD.begin(4)) {
Serial.println("Ah fallado la inicializacion");
while (1);
}
Serial.println("Inicializacion Exitosa");

myFile = SD.open("Ejemplo.txt");

if (myFile) {
Serial.println("Ejemplo.txt:");

myFile.seek(3 * 5);
String lista = myFile.readStringUntil('\r');
Serial.println(lista);

myFile.close();
} else {
Serial.println("Error al abrir Ejemplo.txt");
}
}

void loop() {
}

Serial Print:

Iniciando tarjeta SD...
Inicializacion Exitosa
Ejemplo.txt:
JKL

Ejemplo.txt SD Card

ABC
DEF
GHI
JKL
MNÑ
OPQ
RST
UVW
XYZ

I hope to have solved your doubt! :slight_smile: :slight_smile: :slight_smile:

Wow, thank you very much, it works perfectly! It is exactly what I needed !!!

1 Like

Give a man a fish .......