1

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() {}
lena
  • 730
  • 2
  • 11
  • 23
  • Why are you doing dynamic memory allocation for `arr` and not just `float arr[1000];`? And why do you need space for 1000 floats, if you only read 5? – gre_gor Jan 12 '22 at 07:41
  • Because the number of values in each file 1000 and some time less or more that number , I read 5 that just for example, I will update now. – lena Jan 12 '22 at 07:45
  • Why do you need an array in the first place? – the busybee Jan 12 '22 at 08:18
  • Because I need to send this array to function to do some processing on values and then clear array to be ready to receive the values of the second file, and so on. – lena Jan 12 '22 at 08:25

1 Answers1

1

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;
}
mmixLinus
  • 1,646
  • 2
  • 11
  • 16
  • What about handleArray ? – lena Jan 12 '22 at 09:42
  • @ayla `handleArray()` is what ever you mean when you said *"I want to send this array in each time to a specific function"* This is where you perform your function on the data you read. – mmixLinus Jan 12 '22 at 10:21
  • @ mmixLinus Ok that's right, how can update the function readFile to be more general because now I can not read values like (-0.768). to read any type of data. can you help please! – lena Jan 13 '22 at 12:10
  • @ayla In [this](https://forum.arduino.cc/t/reading-data-from-sd-card-receive-floats/74757/5) thread, the user **system** suggests reading one character a time, appending each character to a string, to read in a full float value. You will need to detect the characters that are NOT part of a float value, like space, braces, newline etc. These will determine when a float starts/ends, and when a new line begins. – mmixLinus Jan 13 '22 at 12:47
  • @ayla You could also do as [here](https://forum.arduino.cc/t/arduino-sscanf/309063) where you read a whole line (or the whole file) into a char array (a string) and use `sscanf(txt, "%f", %val)` to more easily read proper floats. – mmixLinus Jan 13 '22 at 12:50
  • I tried to use in the function readFile() only arr[i++] = f.read(); But it only read one number. Why? – lena Jan 13 '22 at 13:15
  • If you use `file.read()` you will only read one character. According to [this reference](https://www.arduino.cc/en/Reference/FileRead) you can provide a char buffer and a length to read. However, I would suggest you have a look at the [Stream class methods]8https://www.arduino.cc/reference/en/language/functions/communication/stream/) to see what works best for you. Perhaps [readStringUntil()](https://www.arduino.cc/reference/en/language/functions/communication/stream/streamreadstringuntil) is a good choice. – mmixLinus Jan 13 '22 at 13:21
  • Yes, I used that before, but it didn't work. I am very sorry that I took your time.. Could you see the update please? – lena Jan 13 '22 at 13:41
  • The values you got (49,13,10,50,13,10) seem to be what you get if you take *one character* and use it as an integer value. 49 is the character '1', 13 is , 10 is , 50 is '2' etc. So it seems you are reading just one character, and it is being treated as an integer value. Try with `readStringUntil()` – mmixLinus Jan 13 '22 at 14:09