-->
Page 1 of 1

Split a file.txt in multiple packages

PostPosted: Mon Apr 19, 2021 9:06 am
by edward_radical
Hi guys! I’ve got a file.txt saved in my LittleFS memory, which contains 30 lines. I need to iterate over the lines and divide the file in three smaller packages each with 10 lines (in order to send them to the internet). I was trying with this but I don't know if I have to create three different files (output1, output2, output3...) or I can use just one and overwrite it.
Anyone could help me? Thanks a lot

Code: Select allvoid readoutput(){
  File file = LittleFS.open("/file.txt", "r");
  if(!file){
    Serial.println("Error Opening File");
  } Serial.println("Success Opening File");

  File output = LittleFS.open("/output.txt", "w");
    if(!output){
      Serial.println("Error opening Output File");
    } Serial.println("Success opening Output File");

  while(file.available()){
   
      int count = 0;
      Serial.println(count);
      String b = file.readStringUntil('\n');
      if(b){
        if(count == 10){
          output.close();
        }else{output.println(b);}
       
      } else {
        Serial.println("Closing output file...");
        output.close();
      }
    }
 
}//end readoutput