简体   繁体   中英

Read line by line in file and store into string, Flash data saving SPIFFS

I'm working on a project with Flash data saving. I'm using SPIFFS library for ESP32, I'm currently attempting to store the data from each line into a String. Since I have control of how many content can go into the file, it won't need more than 3 Strings to store the data. I could easily manage to store the first line content using readStringUntil . But I can't manage to get the content from 2 and 3 line.

For the first line I'm using this code:

//Pegar a primeira linha do arquivo, onde será armazenado o nome do WIFI (ssid)
void first_line (){
  file = SPIFFS.open("/wifi.txt", "r");

  while (file.available()) {
    String first_line = file.readStringUntil('\n');
    Serial.print(first_line);
    break;
  }

  file.close();
}

I'm writing the code into the File with this function:

// Escrever mensagem dentro do arquivo
void write_file_info(String message) {
  file = SPIFFS.open("/wifi.txt", FILE_WRITE);

  if (!file){
    Serial.println("Error opening file");
    return;
  }else{
    Serial.println("Success opening file");
  }

  if (file.println(message)){
    Serial.println("File was written");
  }else{
    Serial.println("File was not written");
  }

  file.close();
}

And I'm using Append to add the second and third line:

void append_file_info (String message){
  file = SPIFFS.open("/wifi.txt", FILE_APPEND);

  if (!file){
    Serial.println("Erro ao realizar APPEND ao arquivo");
  }

  if (file.println(message)){
    Serial.println("File was added");
  }else{
    Serial.println("File was not added");
  }

  file.close();
}

This is the current output, file size is just for manage and "content inside file" is just for reference:

File size: 37
Content inside file: 
first line
second line
thrid line

This is how I'm reading the file:

void read_file_info() {
  file = SPIFFS.open("/wifi.txt");

  Serial.print("\nFile size: ");
  Serial.println(file.size());

  Serial.print("Content inside file: \n");
  while (file.available()){
    Serial.write(file.read());
  }

  Serial.println("\n");

  file.close();
  delay(3000);
}

I thought on trying to read the data after '\n', but couldn't find any documentation on reading after certain string.
I tried creating a buffer and splitting it later, the output from the buffer is correct but I can't split it into strings correctly:

void second_line (){
  file = SPIFFS.open("/wifi.txt", "r");
  
  char buffer[64];
  while (file.available()) {
   int l = file.readBytesUntil('\n', buffer, sizeof(buffer));
   buffer[l] = 0;
   Serial.println(buffer);
  }

  file.close();
}

It would be simpler using vector:

#include <SPIFFS.h>

using namespace std;

void setup() {
  Serial.begin(115200);

  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  File file = SPIFFS.open("/wifi.txt");
  if (!file) {
    Serial.println("Failed to open file for reading");
    return;
  }

  vector<String> v;
  while (file.available()) {
    v.push_back(file.readStringUntil('\n'));
  }
  file.close();

  for (String s : v) {
    Serial.println(s);
  }
}

void loop() {}

Use v[0] to get first line, v[1] for second line, v[2] for third line and so on.

I could manage to get it working like this:

void all_lines (){
  file = SPIFFS.open("/wifi.txt", "r");
  
  int i = 0;
  char buffer[64];
  String line_one, line_two, line_three;
  
  while (file.available()) {
   int l = file.readBytesUntil('\n', buffer, sizeof(buffer));
   buffer[l] = 0;
   if (i == 0) {
    line_one = buffer;
   }
   if (i == 1) {
    line_two = buffer;
   }
   if (i == 2) {
    line_three = buffer;
   }
   i++;

   if (i == 3){
    break;
   }
   
  }

  file.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM