Think you better that a look at FS.h.But File isn't just a wrapper for SdFat...
It's a generic file handling class. Adding a function to it means every underlying filesystem object must support it. That's exactly the sort of thing that libc's formatted I/O functions are designed to workaround.
// Read in a new line - Note: removes '\r' but leaves '\n'
int bufferLength = origFile.read(lineBuffer, sizeof(lineBuffer));
"SD.sdfs.xxxx()"
You will have more fun with the puppy than with this libraryLooks like everyone is having fun. Would join in but currently distracted from my distractions.
View attachment 38465
#include <SD.h>
#include <Streaming.h>
#define cout Serial
File wrfile;
File rdfile;
void setup() {
Serial.begin(115200);
delay(5000);
SD.begin(BUILTIN_SDCARD);
makeTestFile();
demoFgets();
}
void loop() {
// put your main code here, to run repeatedly:
}
void makeTestFile() {
// create or open test file
if(SD.exists("fgets.txt")) SD.remove("fgets.txt");
wrfile = SD.open("fgets.txt", FILE_WRITE);
// check for open error
if (!wrfile) {
Serial.println("MakeTestFile Error");
} else {
cout << "makeTestFile opened!" << endl;
}
// write test file
wrfile.print(F(
"Line with CRLF\r\n"
"Line with only LF\n"
"Long line that will require an extra read\n"
"\n" // empty line
"Line at EOF without NL"
));
wrfile.close();
}
void demoFgets() {
char line[25];
int n;
// open test file
rdfile = SD.open("fgets.txt", FILE_READ);
// check for open error
if (!rdfile) {
Serial.println("demoFgets error");
} else {
cout << "demo read opened" << endl;
}
cout << endl << F(
"Lines with '>' end with a '\\n' character\n"
"Lines with '#' do not end with a '\\n' character\n"
"\n");
// read lines from the file
while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
if (line[n - 1] == '\n') {
cout << '>' << line;
} else {
cout << '#' << line << endl;
}
}
}
makeTestFile opened!
demo read opened
Lines with '>' end with a '\n' character
Lines with '#' do not end with a '\n' character
>Line with CRLF
>Line with only LF
#Long line that will requ
>ire an extra read
>
#Line at EOF without NL
Tried that. Only works for certain functions. If used with fgets it gives a fgets not a member of sdfs.Have not tested this yet. I'll post back when I have the code uploaded to GitHub...
/**
* Check whether a string matches the current config entry name
* @param[in] itemName The string against which to check the current entry
* @return True if the names match, false otherwise
*/
bool SdConfigFile::checkItemName(const char *itemName) {
// If a matching parameter has already been found, no need to check again
if (paramFound) return false;
// Remove spaces from front of character array
while (currentPos[0] == ' ') currentPos++;
int stringLength = strlen(currentPos);
// Remove spaces, tabs and line ending characters from end of character array
while (stringLength > 0 && discardChar(currentPos[stringLength - 1])) stringLength--;
currentPos[stringLength] = '\0';
// Check if both name strings match and that the string isn't empty
if (strcmp(itemName, currentPos) == 0 && stringLength != 0) {
paramFound = true;
currentPos = strtok(NULL, "=");
if (currentPos) return true;
}
return false;
}
initialization Passed: OK.
lineBuffer = intValue=1234
longValue=12678
floatValue=0.24689
arduinoStringValue=Hello, this is a string with spaces
cStringValue=This is als)
lineBuffer = o a string
boolValue1=True
boolValue2=0
boolValue3=false
ingValue=Hello, this is a string with spaces
cStringValue=This is als)
intValue: 1234
longValue: 0
initialization Passed: OK.
--- Read Method 1 - While Loop ---
lineBuffer = intValue=1234
longValue=12678
floatValue=0.24689
arduinoStringValue=Hello, this is a string with spaces
cStringValue=This is als0)
lineBuffer = o a string
boolValue1=True
boolValue2=0
boolValue3=false
ingValue=Hello, this is a string with spaces
cStringValue=This is als0)
intValue: 1234
longValue: 0
floatValue: 0.00000
boolValue1: 0
boolValue2: 0
arduinoString:
cStringValue:
/**
* Print data to temporary file if tempFile and currentPos.
*/
void SdConfigFile::printLineToFile() {
if ((tempFile) && (currentPos != NULL)) {
tempFile.print(currentPos);
if (equalsSplit) { // If the line was already split
currentPos = strtok(NULL, "="); // Get the value on other side of the equals sign
checkItemName(""); // Strips spaces, etc. from the value
tempFile.print("="); // Add in the equals sign again
tempFile.println(currentPos); // Print the value to the file
}
}
}
/home/wwatson/arduino-1.8.19-TD1.60B5/arduino-builder -dump-prefs -logger=machine -hardware /home/wwatson/arduino-1.8.19-TD1.60B5/hardware -hardware /home/wwatson/Arduino/hardware -tools /home/wwatson/arduino-1.8.19-TD1.60B5/tools-builder -tools /home/wwatson/arduino-1.8.19-TD1.60B5/hardware/tools/avr -built-in-libraries /home/wwatson/arduino-1.8.19-TD1.60B5/libraries -libraries /home/wwatson/Arduino/libraries -fqbn=teensy:avr:teensy41:usb=serial,speed=600,opt=o2std,keys=en-us -ide-version=10819 -build-path /tmp/arduino_build_422842 -warnings=none -build-cache /tmp/arduino_cache_308324 -verbose /home/wwatson/Arduino/SD_fgets_Test/SD_fgets_Test.ino
/home/wwatson/arduino-1.8.19-TD1.60B5/arduino-builder -compile -logger=machine -hardware /home/wwatson/arduino-1.8.19-TD1.60B5/hardware -hardware /home/wwatson/Arduino/hardware -tools /home/wwatson/arduino-1.8.19-TD1.60B5/tools-builder -tools /home/wwatson/arduino-1.8.19-TD1.60B5/hardware/tools/avr -built-in-libraries /home/wwatson/arduino-1.8.19-TD1.60B5/libraries -libraries /home/wwatson/Arduino/libraries -fqbn=teensy:avr:teensy41:usb=serial,speed=600,opt=o2std,keys=en-us -ide-version=10819 -build-path /tmp/arduino_build_422842 -warnings=none -build-cache /tmp/arduino_cache_308324 -verbose /home/wwatson/Arduino/SD_fgets_Test/SD_fgets_Test.ino
Using board 'teensy41' from platform in folder: /home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr
Using core 'teensy4' from platform in folder: /home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr
Detecting libraries used...
/home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/../tools/arm/bin/arm-none-eabi-g++ -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -std=gnu++17 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -Wno-psabi -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=160 -DARDUINO=10819 -DARDUINO_TEENSY41 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH -I/home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr/cores/teensy4 /tmp/arduino_build_422842/sketch/SD_fgets_Test.ino.cpp -o /dev/null
Alternatives for SD.h: [SD@1.2.4 SD@2.0.0]
ResolveLibrary(SD.h)
-> candidates: [SD@1.2.4 SD@2.0.0]
/home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/../tools/arm/bin/arm-none-eabi-g++ -E -CC -x c++ -w -g -Wall -ffunction-sections -fdata-sections -nostdlib -std=gnu++17 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -Wno-psabi -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=160 -DARDUINO=10819 -DARDUINO_TEENSY41 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH -I/home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr/cores/teensy4 -I/home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr/libraries/SD/src /tmp/arduino_build_422842/sketch/SD_fgets_Test.ino.cpp -o /dev/null
Alternatives for Teensy_SdFat.h: []
ResolveLibrary(Teensy_SdFat.h)
-> candidates: []
In file included from /home/wwatson/Arduino/SD_fgets_Test/SD_fgets_Test.ino:1:
Multiple libraries were found for "SD.h"
/home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr/libraries/SD/src/SD.h:27:10: fatal error: Teensy_SdFat.h: No such file or directory
Used: /home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr/libraries/SD
27 | #include <Teensy_SdFat.h>
Not used: /home/wwatson/arduino-1.8.19-TD1.60B5/libraries/SD
| ^~~~~~~~~~~~~~~~
compilation terminated.
Using library SD at version 2.0.0 in folder: /home/wwatson/arduino-1.8.19-TD1.60B5/hardware/teensy/avr/libraries/SD
Error compiling for board Teensy 4.1.
#include <SdFat.h>
#include <Teensy_SdFat.h>
Looks suspiciously similar in parsing although not the same. @Nanoprecision - Have you seen this?Not to throw a wrench into things, but does this library do anything that IniFile doesn't do? That's a pretty well-established standard.
![]()
GitHub - stevemarple/IniFile: Arduino library to parse ini files.
Arduino library to parse ini files. Contribute to stevemarple/IniFile development by creating an account on GitHub.github.com
/**
* Writing method 1 - using a while loop
*/
intValue=1234
longValue=12678
floatValue=0.24689
arduinoStringValue=Hello, this is a string with spaces
cStringValue=This is also a string
boolValue1=True
boolValue2=0
boolValue3=false
yes, I‘ve seen this, but I need to update values and this lib does not support write.Looks suspiciously similar in parsing although not the same. @Nanoprecision - Have you seen this?
Got it.yes, I‘ve seen this, but I need to update values and this lib does not support write.
From the readme.md:
“Write support is a feature that has been requested on several occasions but as I am no longer using the IniFile library I will not add this feature“
initialization Passed: OK.
--- Read Method 1 - While Loop ---
lineBuffer = intValue=1234
lineBuffer = longValue=12678
lineBuffer = floatValue=0.24689
lineBuffer = arduinoStringValue=Hello, this is a string with spaces
lineBuffer = cStringValue=This is also a string
lineBuffer =
lineBuffer = boolValue1=True
lineBuffer = boolValue2=0
lineBuffer = boolValue3=false
lineBuffer =
intValue: 1234
longValue: 12678
floatValue: 0.24689
boolValue1: 1
boolValue2: 0
arduinoString:
cStringValue: This is also a stri
I think you need to actually understand what the FS class is for - what happens when someone calls your new fgets method on an FS implementation (e.g. LittleFS) that doesn't support it? Or even a File object that isn't a currently opened file?Think you better that a look at FS.h.
As I said it is not a new method.your new fgets method
what sketch are you using?Adding fgets() fixes a few of the issues:
No it doesn't. You have it backwards; LittleFS and SdFat are both implementations of the FS interface. Some of the functions may have been "inspired" by the SdFat functions originally but that is because they are generic enough to fit any standard filesystem interface. fgets has nothing to do with filesystems, it is purely a convenience function that operates on top of read/peek. If you implement that one then what's the reasoning for not implementing fprintf, fscanf and all the other libc formatted I/O functions?For instance, if you look in LittleFS.h you will see it uses it own wrapper to SdFat methods.
It will be usable with LittleFS because you've modified the base FS class. It just won't work because only SdFat has it implemented instead of being a generic method. Did you try the readBytesUntil method I mentioned earlier, which would work with any FS implementation?Since I haven't added this fgets littleFs does not support calling fgets!
Does this library do anything that IniFile doesn't? It can store all the same types of data, and it already works.
https://github.com/stevemarple/IniFile
yes, I‘ve seen this, but I need to update values and this lib does not support write.
From the readme.md:
“Write support is a feature that has been requested on several occasions but as I am no longer using the IniFile library I will not add this feature“
Okay, good answer. How many parameters do you need to store, and how often will they be updated?From the readme.md:
“Write support is a feature that has been requested on several occasions but as I am no longer using the IniFile library I will not add this feature“
Okay, good answer. How many parameters do you need to store, and how often will they be updated?