I'm using the SdFat Library's sd.ls(LS_R) function to give me a serial output stream of all files on an SD card. My issue is that I would like to have it use a different character than '\n' at the end of each line as I'm using it as a termchar for my overall comms protocol. How can I get it to print a : or (another symbol that doesn't appear in file names) instead of a \ln


I am currently trying to have fun Modding my own sd.ls().

Worst way, modifying the library:

Go into FatFilePrint.cpp

(Suggestion: Create yourself a new 'flag' like LS_COMMA)

Find the line

void FatFile::ls(print_t* pr, uint8_t flags, uint8_t indent) { ...

add your flag

if (flag & !LS_COMMA) {
} else {
    //original library set of 
    // pr->write(stuff);

--> SuperClass version # (prefered )

this will override the original ls() method by creating a new Class inheriting from the original one.

rather then invoking it from "volume", sd.ls( your params);

invoke it from a directory file instead.

superFatFile rootDir;
rootDir.ls( __your_params__);

Place this superClass declaration in your .h don't hesitate to remove any of the "flags" related code if YOUR requirement never calls for them.

class superFatFile : public FatFile {
    void ls(print_t* pr, uint8_t flags /* = 0 */, uint8_t indent /* = 0 */){
        superFile file;
        while (file.openNext(this, O_READ)) {
            if (!file.isHidden() || (flags & LS_A)) {
                // adds 'spaces' indent for dir level
                for (uint8_t i = 0; i < indent; i++) {
                    pr->write(' ');

                if (flags & LS_DATE) {
                    pr->write(' ');

                if (flags & LS_SIZE) {
                    pr->write(' ');

                // prints filename
                if (!file.isDir()) {    
                    pr->write('\n');    // HERE IS YOUR SEPARATOR CARACTERS !!!

                // execute recursivly if LS_R flag was set
                if ((flags & LS_R) && file.isDir()) file.ls(pr, flags, indent + 2);


    } // end of new LS method
}; // end of superClass
  • Nice, I ended up changing my termchar – ATE-ENGE Oct 11 '17 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.