fgets alternative

Is there a good alternative for fgets in Arduino C? I'm using it to read data from aFILE* type file but this function is really slow, it takes from 30 to even 150 ms.

michalt38:
Is there a good alternative for fgets in Arduino C? I'm using it to read data from aFILE* type file but this function is really slow, it takes from 30 to even 150 ms.

This might help.

It is a wrapper class for FILE that includes a collection of convenient read functions.

Try it out and see if it works for you.

For reasons of library conflicts, I renamed my SD library to "MySD".

So you will need to change #include <MySD.h> to #include <SD.h>

But that should be the only compile error you get.

Just add the .cpp and .h files to your sketch folder.

Sorry ignore that previous version of TextFile.h/.cpp - I forgot that it is using my customised CString class.

I have changed it to use the regular Arduino String class and removed references to all my other custom classes.

The functions in that class you will be interested in are :

  bool readWord(String& strWord); // Read characters into the string until a white space character is read.
   
   
   bool readWord(String& strWord, const char cDelim); // Read characters into the string until the specified delimiting character(other than white space) is read.

   bool readLine(String& strLine, const bool bIncludeEOL = false, const bool bDebug = false); // Read the entire line into the string, bIncludeEOL specifies whether or not to add the \r\n chars to the string.
// If you pass in true for bDebug then the function readLine(...) will dump each line it reads to serial monitor should you want to debug the contents of your file.

TextFile.cpp (4.46 KB)

TextFile.h (3.46 KB)