Unexpected variable values reading from file (ESP32)

  arduino, arduino-ide, c++, esp32

I am still learning Cpp, so please advise if I am misunderstanding here.

Using an ESP32, I am trying to read / write files to Flash / FFat. This is the method I have created which should read a file from flash and load it into PSRAM:

unsigned char* storage_read(char* path) {

    File file = FFat.open(path);
    if(!file) {
        Serial.println("no file");
        return 0x00;
    }

    int count = file.size();
    unsigned char* buffer = (unsigned char*)ps_malloc(count);

    Serial.printf("Bytes: %dn", count);
    Serial.printf("Count: %dn", sizeof(buffer));

    for (int i = 0; i < count; i++) {
        buffer[i] = (unsigned char)file.read();
    }

    file.close();
    return buffer;
}

The problem is that I get the contents of my b64 data file, with the addition of several extra bytes of data globbed on the end.

Calling the method with:

Serial.printf("Got: %s", storage_read("/frame/testframe-000.b64"));

I get the output:

Bytes: 684
Count: 4
Got: <myb64string> + <68B of garbage>

Why would sizeof not be returning the proper size?

What would be the proper way of loading this string into a buffer?

Source: Windows Questions C++

LEAVE A COMMENT