Using SdFat.h to read a list stored on Sdcard one line at a time

I have a list of songs on an Sdcard stored in a file called TITLES.txt
Each song title ends in a carriage RTN and line feed

I want to read in one song title on the list and act on it.
Then read in the next song title on the list and act on it. etc.

The only examples that I can find read in the whole list at once.
Can someone show me a simple way of doing this using the SdFat.h library

I want to stay with the SdFat.h library because my other code depends on it.

Thanks.

Explain what you want to do exactly, because if you want to modify the title in the file, you will have to read and rewrite everything in the file, after that title (unless all titles are always the same length)

Anyway, if you want to read one line, you use the read() method in a while loop, until the result is a newline character

Here is an example I wrote for another topic t1054099 - Wokwi ESP32, STM32, Arduino Simulator

And another, in case your lines are always the same length t1049536.ino - Wokwi ESP32, STM32, Arduino Simulator

I just want to read a line that can vary in length. Someone suggested this code but I get an
error: 'class SdFile' has no member named 'readStringUntil'

I was trying to avoid reading in each character and checking for a \n but maybe you can't do what the other person suggested

this code worked to print out the first text line in the file. If I add while (myFile.available()) then all three lines get printed. thanks for helping me.
image

That's a pretty good summary of how readStringUntil works.

Any particular reason you are trying to avoid this way of reading?

That sounds about right. The while statement in this situation is saying keep going till the end of the file is reached.

What did you want to do with the song name(s)?

Finally, I would avoid using the String class and stick to simple c strings - i.e. Null terminated character arrays to avoid memory issues.

Once I get the song name from the sd card, I then want to pass it on to a webpage using Soft Access point protocol. Then when I select the SONG it will then play that tune .

I can't figure out how to insert the contents in the buffer into the HTML code. Currently it shows buffer on the webpage. Any ideas?

image

Someone suggested the following which works but seems really clunky.
There should be a way just to insert the contents of the buffer into the one line of code

          buffer = titlesFile.readStringUntil('\n');
            fred = "<a href=\"/1\"><var>";
            alan = "</var></a><br><br>";
            client.print(fred + buffer + alan);

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