strange characters reading sd file

Hello,

When I try to retrieve data from my sd card, I sometimes receive weird characters on my Serial monitor, but not all the time.

For example:

#include <SD.h>

File myFile;

#define pinCS 53    // SD-Card CS-pin

void setup() {
  Serial.begin(9600);
  pinMode(pinCS, OUTPUT);

  // SD Card Initialization
  if (SD.begin(pinCS)) {
    Serial.println(F("SD card is ready to use."));
  }
  else {
    Serial.println(F("SD card initialization failed"));
    return;
  }
  
  char sdString[38];
  myFile = SD.open("testfile.txt");
  if (myFile) {
    myFile.read(sdString, sizeof(sdString) - 1);
    Serial.println(sdString);
    myFile.close();
  }
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

Will give this as a result on my Serial monitor:

SD card is ready to use.
18-07-05,00:00,23.00,24.00,26.00,6.15⸮"

the reversed question mark and " shouldn’t be there.

When a run the whole myFile.read routine twice, I get a correct output.

#include <SD.h>

File myFile;

#define pinCS 53    // SD-Card CS-pin

void setup() {
  Serial.begin(9600);
  pinMode(pinCS, OUTPUT);

  // SD Card Initialization
  if (SD.begin(pinCS)) {
    Serial.println(F("SD card is ready to use."));
  }
  else {
    Serial.println(F("SD card initialization failed"));
    return;
  }
  
  char sdString[38];
  myFile = SD.open("testfile.txt");
  if (myFile) {
    myFile.read(sdString, sizeof(sdString) - 1);
    Serial.println(sdString);
    myFile.close();
  }
  myFile = SD.open("testfile.txt");
  if (myFile) {
    myFile.read(sdString, sizeof(sdString) - 1);
    Serial.println(sdString);
    myFile.close();
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

Serial monitor:

SD card is ready to use.
18-07-05,00:00,23.00,24.00,26.00,6.15
18-07-05,00:00,23.00,24.00,26.00,6.15

I’m not sure what I’m doing wrong.
I hope someone can point me in the right direction…

I’ve included the testfile as attachment.

testfile.txt (28.7 KB)

This is some of what you posted as your testfile.txt on the sd card.

18-07-05,00:00,23.00,24.00,26.00
18-07-05,00:05,23.00,24.00,26.00
18-07-05,00:10,23.00,24.00,26.00
18-07-05,00:15,23.00,24.00,26.00
18-07-05,00:20,23.00,24.00,26.00

Where does the 6.15 shown here come from?

18-07-05,00:00,23.00,24.00,26.00,6.15

How is testfile.txt written to the card? Is there a new line terminator? Is each line written as a character array with a null terminator (c-string) constructed using sprintf for example, or just as an array of chars?

If testfile.txt actually contains lines of data including the 6.15 terminated with a newline('\n'), then the the line is indeed 38 characters. 37 data + newline

I do not believe that your reading routine of 37 bytes into sdString properly null terminates what you read so that it can be printed with the Serial.print(sdString).

char sdString[38];//sizeof returns 38 with array indexes 0 - 37.
  myFile = SD.open("testfile.txt");
  if (myFile) {
    myFile.read(sdString, sizeof(sdString) - 1);//read 37 bytes into sdString
    sdString[37] = '\0'; //null terminate
    Serial.println(sdString);
    myFile.close();
  }

I also think that if you declare sdString as follows the compiler will null terminate.

char sdString[38] = "";

It's not clear to me how you plan to adapt the reading routine to read multiple lines of the file.

I accidentally attached the wrong testfile.txt; an older one without the 6th field.

Declaring sdString as you proposed did the trick.

Thanks a lot!