arduino-libraries / SD

SD Library for Arduino
http://arduino.cc/
GNU General Public License v3.0
179 stars 155 forks source link

FILE_WRITE | O_TRUNC leaves extra bytes #138

Closed xtnctx closed 11 months ago

xtnctx commented 11 months ago

I am trying to update my file by deleting all contents then writing a new one. I come up with this solution:

myFile = SD.open("info.h", FILE_WRITE | O_TRUNC);
if (myFile) {
  myFile.println("newtext123");
  // ...
}
myFile.close();

However, when I read the contents, there is an extra 13 and 10. After looking in ascii table, it means carriage return and line feed respectively. So I have to skip those two.

while (myFile.available()) {
  uint8_t readByte = myFile.read();
  // Skip carriage return (CR) and line feed (LF) characters
  if (readByte != 13 && readByte != 10) {
    Serial.println(readByte);
  }
}

Is having an extra 13 and 10 normal?

per1234 commented 11 months ago

Hi @xtnctx

Is having an extra 13 and 10 normal?

Yes, when you use println:

https://www.arduino.cc/reference/en/libraries/sd/println/

Print data, followed by a carriage return and newline, to the File

If you don't want it, use myFile.print("newtext123") instead