0

I'm codding for Arduino (ESP8266), and have to read a string from a file, to use it. I don't know how long is that file, so I have to create a char* and pass it to the readConf function so that malloc decides for the memory size.

void readConf(char path[], char **buff){
    SPIFFS.begin();
    if (SPIFFS.exists(path))
    {
        File file = SPIFFS.open(path, "r");
        int size = file.size();
        Serial.print("File size: ");
        Serial.println(size);
        char *bu;
        bu = (char*) malloc((size+1) * sizeof(char));
        file.read((uint8_t*) bu, size);
        bu[size] = '\0';
        Serial.print("Data: ");
        for (int i = 0; i < size; i++)
            Serial.print(bu[i]);
        Serial.println("");
        //Everything is OK. It is printed correctly.
        buff = &bu; //!This is the problem!
        file.close();
    }
    SPIFFS.end();
}

#define file_path "/file"
void setup(){
    
    if(WiFi.getMode() != WIFI_STA)
        WiFi.mode(WIFI_STA);
    
    char* username;
    readConf(file_path, &username);
    char* password;
    readConf(file_path, &password);
    /*The same with password. */
    WiFi.begin(username, password);
    
    Serial.print("username: ");
    Serial.println(username); //Here I sometimes get Exception, and sometimes prints non-sense characters
    free(username); //My memory is limited. I'm doing all this stuff for this line!
    //...
}

I also searched a lot in StackOverflow and other sites, also used char *, char **, filling the pointer directly in the readConf function, and a lot more but non of them worked. How should I deal with it? Can I do it at all?

NOTE: I should not use String class.

Mohammad Kholghi
  • 533
  • 2
  • 7
  • 21
  • 1
    Try `*buff = bu` instead. Or if you're really programming in C++ use *actual* references instead of pointers to emulate references. – Some programmer dude Jul 10 '21 at 09:47
  • @Someprogrammerdude I used to get a lot of hardware exceptions, as a result, Arduino would restart again and again. I have to use it this way. – Mohammad Kholghi Jul 11 '21 at 05:39
  • By the way, why are you now using the Arduino standard `String` class for your strings? – Some programmer dude Jul 11 '21 at 07:48
  • @Someprogrammerdude Hardware exceptions:)) I had the same error as: https://stackoverflow.com/questions/60966280/esp8266-repeatedly-throws-exception0, so as https://stackoverflow.com/a/60969009/9691976 says: `This error pattern is typical for the String class.` – Mohammad Kholghi Jul 11 '21 at 07:54

2 Answers2

1

The function parameter buff is a local variable of the function

void readConf(char path[], char **buff){

So changing it within the function

buff = &bu; //!This is the problem!

has neither effect on the variable password declared in the function setup

char* password;
readConf(file_path, &password);

You need to write

*buff = bu;

That is you need in the function readConf to change the value of the pointer password declared in the function setup

So you passed the pointer to the function readConf by reference through a pointer to it

readConf(file_path, &password);

Now to get a direct access to the original pointer password you need to dereference the parameter buff that was initialized by the expression &password.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

This function will return size of file in bytes.

#include <fstream>

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

char* buff; - that is pointer to char array. Not char** buff;

vova
  • 63
  • 8