Possible to load variables from a file on SPIFFS?

I'm wondering if it's possible to load variables directly from a file on SPIFFS?

For example, suppose I have a file with the contents:

int a = 1;

int b = 2;

Can I load those variables directly into my script, so I can for example run:


Thanks for any help.

I would say that depends on the SPIFF API. Which microController are you using?

Sorry I should have said, using an Espressif ESP32. Using the SPIFFS.h library.

It depends what you mean by “directly”.

You could load the file from SPIFFS, and parse it, and get the values, and use them in a sketch.

I don’t know of a way a running sketch could declare arbitrary new variables based on file input though

Here is all the dope on the ESP32 SPIFF API SPIFFS Filesystem - ESP32 - — ESP-IDF Programming Guide latest documentation. You might notice that the SPIFF API is all about managing the SPIFF's and not managing the data in the SPIFF's. As was wrote above, you'll have to work out your own parsing code.

I do not quite understand what you try to achieve.

Let's say that you manage to do what you want to do, what are you going to do with it? For your sketch to do something useful with it (e.g. if (a==1) ), the compiler needs to know about it.

If you want to store values in a file and assign them to a variable in your compiled sketch, then the answer is that it is possible.

Note: never used an ESP.

For example, suppose I have a file with the contents:

int a = 1;

int b = 2;

Can I load those variables directly into my script

No, at least not like that.

Remember that C/C++ is a compiled language. Each code line in your sketch is translated into low-level machine code before the sketch is uploaded to the "Arduino" (in this case an Arduino-compatible ESP32 board). The Arduino has no C compiler of its own, it relies on the PC to do that before the upload. So you can't put lines of C code into a file, to be read by the Arduino and compiled, it can't do that.

But what you could do is put into your file:


and then your sketch can open that file and read the first line into variable a and the second line into variable b.

ESP doesn’t have user EEPROM, so the OP is probably trying save and/or restore variables between runs.

If you read up on how to use EEPROM, and SPIFFS EEPROM emulation you’ll go far.