Read multiple text file in Arduino ide

Viewed 167

I have folder contain n txt file(files have numeric values) I need to read these files and save the file contains in array. How can I read the contents of the first file1 in the array and then clear the array ,and then read the second file2 in the same array and so on?

#include "SPIFFS.h"
void setup() {
  Serial.begin(115200);
 float *arr=(float*)malloc(1000*sizeof(float));
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  } 
  File root = SPIFFS.open("/");
  File file = root.openNextFile();
  while(file){
    for(int i=0;i<1000;i++){
        arr[i] = file.parseFloat();
        Serial.println(arr[i]);}
      //Serial.println(file.name());
       //file = root.openNextFile();
  }
}
 
void loop() {}
1 Answers

You could do something similar to this. Though you will need to check the proper behaviour of parseFloat() when it doesn't detect a float value, ie., what it actually returns.

while(file) {
    int length = readFile(arr, file);
    file.close();
    handleArray(arr, length);
    file = root.openNextFile();
}

...

int readFile(float *arr, File f) {
    int i = 0;
    float val = 0.0;
    val = f.parseFloat();   // returns 0.0 when no float was found?
    while (val > 0.0) {
        arr[i++] = val;
        val = f.parseFloat();
    }
    return i;
}
Related