Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Having issue to read the SPIFF file. #14

Closed
OttoKlaasen opened this issue Apr 12, 2020 · 1 comment
Closed

Having issue to read the SPIFF file. #14

OttoKlaasen opened this issue Apr 12, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@OttoKlaasen
Copy link

Dear Sir,
following code does not seem to work:

// we could open the file
size_t size = f.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// Read and store file contents in buf
f.readBytes(buf.get(), size);

I get rubish in buf, i had to rewrite like this to be able to read by SPIFF file:

File f = SPIFFS.open(CONFIG_FILE, "r");
String buffer; /// Create a string buffer to get the file contents
//
Serial.println("Get file");
if (!f) Serial.println("FAIL TO LOAD FILE");
while (f.available()){
buffer = f.readString().c_str();
}
Serial.print("file buffer: ");Serial.println(String(buffer)); // Print buffer which contains file contents

char * doc = new char [buffer.length()+1]; // Define a pointer to a char buffer called doc
std::strcpy (doc, buffer.c_str()); // Copy string buffer to char buffer caled doc

Serial.print("doc: ");Serial.println(String(doc)); // Print doc as string
// Do the joson stuff
StaticJsonDocument<1024> json;
Serial.println("deserialize !");

of course add this in a header file:
// strings and c-strings
#include
#include
#include

Cheers and greetings from the netherland.

@khoih-prog
Copy link
Owner

Hello,
Thanks for using the library and finding out the bug/mistake.
The issue is that the buffer is not allocated large enough, lacking one NULL terminating byte.

Please change the corresponding code of these affected examples

  1. ConfigOnSwitchFS
  2. ConfigPortalParamsOnSwitch
    to
// we could open the file
    size_t size = f.size();
    // Allocate a buffer to store contents of the file.
    std::unique_ptr<char[]> buf(new char[size + 1]);

from

// we could open the file
    size_t size = f.size();
    // Allocate a buffer to store contents of the file.
    std::unique_ptr<char[]> buf(new char[size]);

The AutoConnectWithFSParameters is still OK with correct code

        size_t configFileSize = configFile.size();
        Serial.println(configFileSize);

        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[configFileSize + 1]);

Your solution is working OK and you can keep using it.
Those examples will be uploaded to the Master, and included in the next Release.

Regards,

khoih-prog added a commit that referenced this issue Apr 12, 2020
Fix [Having issue to read the SPIFF file](#14) for
1. [ConfigOnSwitchFS](examples/ConfigOnSwitchFS)
2. [ConfigPortalParamsOnSwitch](examples/ConfigPortalParamsOnSwitch)
khoih-prog added a commit that referenced this issue Apr 13, 2020
### Releases 1.0.7

1. Use `just-in-time` scanWiFiNetworks() to reduce connection time necessary for battery-operated DeepSleep application. Thanks to [CrispinP](https://github.com/CrispinP) for identifying, requesting and testing. See [Starting WiFIManger is very slow (2000ms)](#6)
2. Fix bug relating SPIFFS in examples :
 - [ConfigOnSwitchFS](examples/ConfigOnSwitchFS)
 - [ConfigPortalParamsOnSwitch](examples/ConfigPortalParamsOnSwitch)  (now support ArduinoJson 6.0.0+ as well as 5.13.5-)
 - [AutoConnectWithFSParameters](examples/AutoConnectWithFSParameters)
 See [Having issue to read the SPIFF file](#14), Thanks to [OttoKlaasen](https://github.com/OttoKlaasen) to report.
3. Fix [README](README.md). See [Accessing manager after connection](#15)
@khoih-prog khoih-prog added the bug Something isn't working label Apr 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants