Sorry guys I need a little bit of help again

Processor: ESP 8266 development board

Problem trying to use LittleFS and formatting int. If the number has two digits no problem, but numbers having only one digit the alignment changes.

I’m probably doing something very silly, but if anyone can point me in the right direction I would very much appreciate it.
I have tried using int & int_least32_t & looked at a number of articles and I have probably missed the point.

#include "LittleFS.h"

bool    LittleFSActive = false;

#define TESTFILE "/testfile.txt"

String My="";

int_least32_t Day=17;

int_least32_t Hour=18;

int_least32_t Min=16;

int_least32_t Sec=30;

void setup()

{

  Serial.begin(115200);

  delay(1000);

  

  // Start filing subsystem

  if (LittleFS.begin()) {

      Serial.println("SPIFFS Active");

      Serial.println();

      LittleFSActive = true;

  } else {

      Serial.println("Unable to activate SPIFFS");

  }

  //*

  char filename [] = "/testfile.txt"; 

  if (LittleFS.exists(filename)) LittleFS.remove(filename); // First in this example check to see if a file already exists, if so delete it

  File myDataFile = LittleFS.open(filename, "a+");        // Open a file for reading and writing (appending)

  if (!myDataFile)Serial.println("file open failed");   // Check for errors

  

  myDataFile.println(Day);

  myDataFile.println(Hour);

  myDataFile.println(Min);

  myDataFile.println(Sec);

  Serial.println(myDataFile.size());                    // Display the file size (26 characters + 4-byte floating point number + 6 termination bytes (2/line) = 34 bytes)

  myDataFile.close();      

  

 //*/

}

void loop()

{

  if (LittleFSActive) {

    if (LittleFS.exists(TESTFILE)) {

      /*

      File f = LittleFS.open(TESTFILE, "r");

      if (!f) {

        Serial.print("Unable To Open '");

        Serial.print(TESTFILE);

        Serial.println("' for Reading");

        Serial.println();

      } else {

        String s;

        Serial.print("Contents of file '");

        Serial.print(TESTFILE);

        Serial.println("'");

        Serial.println();

        while (f.position()<f.size()){

          s=f.readStringUntil('\n');

          s.trim();

          Serial.println(s);

        } 

        f.close();

      }

      Serial.println();

            

      f = LittleFS.open(TESTFILE, "a");

      if (!f) {

        Serial.print("Unable To Open '");

        Serial.print(TESTFILE);

        Serial.println("' for Appending");

        Serial.println();

      } 

      else {

        Serial.print("Appending line to file '");

        Serial.print(TESTFILE);

        Serial.println("");

        Serial.println();

        //f.println("This line has been appended");

        Serial.println(f.size());

        f.close();

      }

      */

     String Temp="";

     File f = LittleFS.open(TESTFILE, "r");

      if (!f) {

        Serial.print("Unable To Open '");

        Serial.print(TESTFILE);

        Serial.println("' for Reading");

        Serial.println();

      } else {

        String s;

        Serial.print("Contents of file '");

        Serial.print(TESTFILE);

        Serial.println("' after append");

        Serial.println();

        while (f.position()<f.size())

        {

          s=f.readStringUntil('\n');

          //s.trim();

          Serial.println(s);

          My=My+s;

        } 

        f.close();

        Temp=My[0];

        Temp=Temp+My[1];

        Day=Temp.toInt();

        Temp=My[2];

        Temp=Temp+My[3];

        Hour=Temp.toInt();

        Temp=My[4];

        Temp=Temp+My[5];

        Min=Temp.toInt();

        Temp=My[6];

        Temp=Temp+My[7];

        Sec=Temp.toInt();

        Serial.print("Day: ");Serial.println(Day);

        Serial.print("Hour: ");Serial.println(Hour);

        Serial.print("Min: ");Serial.println(Min);

        Serial.print("Sec: ");Serial.println(Sec);

        Serial.println("");

        Serial.println("*");

      }

   

    } else {

      Serial.print("Unable To Find ");

      Serial.println(TESTFILE);

      Serial.println();

    }

  }

  

  while (true){

    yield();

  }

}

Can you be more specific:

  • What is the problem? How does it manifest itself?
  • What do you expect to happen? What happens instead?
  • What numbers are you referring to?

HI thank you for your time, and I will try to explain
at the top of the code you will see 4 int’s all I’m trying to do is to put them into flash memory the value of each, Day,Hour,Min,Sec. then after a reboot or power off situation I would like to recover the values.

What actually happens that upon the recovery phase depending on the content of each of the four int’s using:
Temp=My[0];
Temp=Temp+My[1];
Day=Temp.toInt();

But if Day=1 it only returns obviously a single value, but then the placement alters for the next value:
Temp=My[2];
Temp=Temp+My[3];
Hour=Temp.toInt();

I need to have two digits to retrieve the correct values for each int.
Hence the reason for using int_least32_t Day=1; I was hoping this would force a constant size to the return value.

I just would like to recover the four values from flash memory and put the correct value back into each int.

Hope that helps
if not please shout back and I will try to elaborate.

Thank you

The main problem is this code:

s=f.readStringUntil('\n');
My=My+s;

readStringUntil will discard the newline character. So the variable s just contains a number. All the numbers are then concatenated into a single string My. So My will eventually contain 17181630. Thus, the separation between the four values is lost.

So reading needs to be changed such that the separator is no longer lost. Instead of:

while (f.position()<f.size())
...
Sec=Temp.toInt();

Just do:

String s;
s = f.readStringUntil('\n');
Day = s.toInt();
s = f.readStringUntil('\n');
Hour = s.toInt();
s = f.readStringUntil('\n');
Min = s.toInt();
s = f.readStringUntil('\n');
Sec = s.toInt();
f.close();

thank you for your speedy reply manuelbl
that has certainly corrected the problem I was experiencing. I must have spent hours on that yesterday and you managed to answer it in just a few minutes. Anyway I have learnt a bit more so now I can go and create some more brain tangling errors.(Sometimes I think I must be mad!!)
Thank you again for your time