Confusion over data read via serial communications

First, I should point out that I'm brand new to C++ programming. Started last week :-)

I have a little bit of confusion regarding data read via serial communications.

In my case, I am reading a text file from an SD card like this:

temp = myFile.readStringUntil('\n');

The result is that the string temp will hold an entire line of text read from the SD card.

Let's assume that the line read from the file has the text "SSID:MySSID".

If I perform the following comparison, it fails:

if (temp == "SSID:MySSID") { Serial.println ("Comparison is TRUE"); }

Investigatng further, I ran this code:

Serial.println (temp.length);

The result is 12 which is one longer than I was expecting.

If I strip off the last character of the string, my test passes, so clearly there is some extra character at the end of my string.

QUESTION: I'm guessing that this is an indication that there is a NUL character at the end of the string. Can anyone confirm whether this is true? Also, is there a way for me to print the ASCII value of each character?

If it does turn out that there is a NUL at the end of the string, would anyone be able to explain the reasoning behind this?

As I noted, I'm new to programming in C++. My only real programming experience is just simple BASIC like languages such as QB64 so I'm very familiar with basic programming concepts, but I've never encountered anything that sticks NULs onto the end of strings of data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

What Arduino are you using?


Is there a possibility that the line ends with a carriage return + line feed, instead of just the line feed?

Thanks for the info. I'll have to read up on cstrings. I find it amazing that a feature such as strings could cause any sort of corruption, but I'll take your word for it.

As for the type of Arduino I'm using, I started with an UNO a week ago where I began my journey with Arduino. I experimented with SD card readers, WiFi module, real time clock, and of course all the simple basics like servos, some simple digital and analog circuits, etc.

Now I'm starting my first real project but just the libraries I need are sucking up all my SRAM space for variables so I had to switch to a MEGA 2560.

This project incles an SD card, WiFi module, and a real time clock module so I have to load the wire library, SPI, SD, Real time Clock, SoftwareSerial, and LiquidCrystal libraries.

As for the other question: I should point out that I tried reading the lines from the file by checking for either a CR or LF with the same results. So, the line should end without either of those characters.

So that brings me back to a previous question: Is there any way to print the ASCII code for each character in the string?