-
My goal is: to save some data that I get from a sensor every 10 minutes in a “.txt” file which is located in the fs directory inside my project.
-
My actions are: I use basic C commands to write text inside a .txt file as following:
enum mgos_app_init_result mgos_app_init(void) {
FILE *fp;
fp = fopen(“test.txt”, “w+”);
fprintf(fp, “This is testing for fprintf…\n”);
fclose(fp);
return MGOS_APP_INIT_SUCCESS;
} -
The result I see is: the “test.txt” file in the FS directory, does not perform the text changes, so it remains in the initial state (empty).
-
My expectation & question is: When I try to read text from this file, I can do it successfully, but I can not write into this file. Can you please help me to solve this situation? I am new in Mongoose OS so thank you in advance.
Did you check fp
for not being NULL ?
Did you think on using specific logging functions ? here and here
Thank you for your reply,
What I wanted to do is to create a json file with some data that I receive from my sensor. Now I understood that the file is created but I can not see it in the directory like index.html. I can only see the file listed using:
mos ls
mos get test.json
I thought that there was some method that I could access the file also in Finder (because I use Macbook), in order to see other details of the file like the size etc.
The microcontroller supports a basic file system, but there is no mounting to your machine’s operating system, that is, it does not show up on your machine’s file system.
Access to the file system is provided by means of an RPC API. Unless someone writes a driver for your OS, you’ll have to use that API through its transport channels (mos tool uses them), to access files in the microcontroller.
Be aware that there are contention issues involved, the microcontroller can be writing to the file while you intend to read through the API, so keep that in mind
I don’t understand, are you trying to write to the flash on an ESP32 module, or an SD card? Something else?
you have a lot of sense, going to test
this works fine
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
then
mos get example.txt
or
mos ls
also you can use this instruction
json_fprintf("settings.json", "{ a: %d, b: %Q }", 123, "string_value");
then
mos get settings.json
OUTPUT
mos get settings.json
Using port /dev/ttyUSB0
{ "a": 123, "b": "string_value" }