Issue reading file with File.readstring

Good day
I am close to a newbie trying to look and understand various examples.
\this one has me beat at the moment it's just related to saving and reading a config file in a Littlefs file system.
File appears to write correctly, proven by ESP32OTA which lets you read and edit the file.
however the file never reads correctly.
file appears to open correctly.
file.available reports 40 bytes before the readstring()
file.available reports 0 after the read string.
but contents of FileText string isn't changed!
There has to be a coding issue but I just cannot see it. A load of examples and documentation appear to use similar code.
thanks for reading

String readFile(String filename){
    File file = LittleFS.open(filename);
    if(!file){
        Serial.println("Failed to open file for reading");
        return "";
    }
    
    String fileText = "hello world";
 Serial.printf("file contents %s\n",fileText);
  
    while(file.available()){
        Serial.printf ("file available : %i\n",file.available());
        String fileText = file.readString();
        Serial.printf ("file available : %i\n",file.available());
    }
    file.close();
    Serial.printf("file contents %s\n",fileText);
    return fileText;

You have another variable called fileText which is only in scope within the while loop, so it is not the variable you return.

Lose the "String" within the while loop

Thank you I will experiment further on that.
My programming knowledge goes back to K&R.
I knew it had to be something basic I and the author had missed
Graham

Scope was the same back then.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.