Append to file - strange behavior when flashing with PlatformIO - Arduino IDE works fine

Hi

I have a problem with logging functionality in my app, when I’m flashing firmware using Arduino IDE everything works fine. But when I’m doing it with PlatformIO on the begining it’s fine, but after like 10/20 seconds I’m getting strange end of line characters added:

0;0;0;0;���0;0;0;0;���0;0;0;0;0
0;0;0;0;0
0;0;0;0;���0;0;0;0;���0;0;0;0;0

Here is my code:

#include <FS.h>
#include <Arduino.h>


class StillDataTaskClass
{
public:
    StillDataTaskClass();
  void exec();
private:
    String _fileName = "/data.csv";

    int _printTime = 0;
};


  auto *stillDataTask = new StillDataTaskClass();

void setup()
{

  Serial.begin(115200);


}

void loop()
{
  delay(1000);

  stillDataTask->exec();
}

StillDataTaskClass::StillDataTaskClass()
{

        SPIFFS.begin();
        SPIFFS.remove(_fileName);
}

void StillDataTaskClass::exec()
{
    char csvEntry[300];
    sprintf(csvEntry, "%i;%i;%i;%i;%i", 0, 0, 0, 0, 0);
    
    
    File file = SPIFFS.open(_fileName, "a");

    if (!file)
    {
        Serial.println(F("Failed to create file"));
        return;
    }

    file.println(csvEntry);

    file.close();

    if ((millis() - _printTime) > 5000)
    {
        _printTime = millis();
        auto fileRead = SPIFFS.open(_fileName, "r");
        Serial.println(fileRead.readString());
        fileRead.close();
    }

}

My Arduino IDE settings:

My PlatformIO settings:

[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
platform_packages =
framework-arduinoespressif8266 @ https://github.com/esp8266/Arduino.git
toolchain-xtensa@2.100100.200706

upload_speed = 115200
upload_port = COM12
monitor_port = COM12
monitor_speed = 115200

It might be helpful to see those weird characters in hexadecimal?

sprintf(csvEntry, "%x;%x;%x;%x;%x", 0, 0, 0, 0, 0);

In theory, they should always be zeros, but when it gets weird, who knows?

Edit: Removed additional double quote. My bad!

Cheers,
Norm.

0;0;0;0;���0;0;0;0;���0;0;0;0;0

Still strange :slight_smile:

I found out by accident that if I will add space as a fist character problem does not appear :wink:

Hmm. Weirder and weirder!

I’m afraid I have no idea why this should be happening, or why the space makes it work. Sorry! :frowning_face:

You don’t have other code that might be writing to csvEntry so I can’t see where the corruption is coming from.

Given that the last of 5 zeros is affected, but ends up being three characters, itmakes no sense.

Mind you, ypu never seem to write anything othere than 5 zeros, so why not just:

void StillDataTaskClass::exec() {
    char *csvEntry = "0;0;0;0;0";
    ....
}

Cheers,
Norm.