In the examples for writing to SIPFFS (and SD cards), the functions for writing or appending require a const char ” message. So doing something like writeFile(SPIFFS, “/hello.txt”, “Hello “); meets the need as “Hello” can be considered a const char type. How do I send a number (e.g. the value of a counter declared as an integer). I want to writeFile(SPIFFS, “/hello.txt”, counterValue); how do I type-change the number to a const char ?
A related question is in a LoRa receiver code, with a statement like the following:
String LoRaData = LoRa.readString();
Serial.print(LoRaData);
When I try to write to an SD card using writeFile(fs::FS &fs, const char * path, const char * message) with LoRaData as the message, it complains that it’s not a const char. How do I type-change the LoRaData to a const char ?
I suspect this is a beginner C question, but I’m stuck here. Thanks for any help.
… a solution is to change the const char * message in the writeFile or appendFile function to String message. Then elsewhere (i.e. in setup or loop), convert the integer number to a string via: String stringNumber = (String) number; where number is declared as int number and is used as a counter, for example. Thus it’d be: writeFile(fs::FS &fs, const char * path, String message) and this works fine.
However, it would be helpful to me to understand how to use the const char * message …. maybe a lesson in c pointers? Somebody wrote that code for a reason I assume! Thanks for any help here … but for the moment I have a solution I think.
Hi Joe.
You can pass the counter (int variable as follows).
String(counter).c_str()
Here’s some information about c_str(): https://cplusplus.com/reference/string/string/c_str/
To be honest, I don’t think I’m the best person to teach about C pointers.
Here are some resources about pointers that I think will help you understand:
- https://www.geeksforgeeks.org/features-and-use-of-pointers-in-c-c/
- https://www.tutorialspoint.com/cprogramming/c_pointers.htm
- https://www.w3schools.com/c/c_pointers.php
I hope this helps.
Regards,
Sara
Thank you Sara !! … you’re a real help 🙂 I really like python because it is so much easier to play with data types and lists (and it has the try/except). C code is great for full-time programmers who live it day after day, but for occasional users, it’s a occurring steep learning curve for types, pointers, structures, etc. …in my opinion.
I questioned why the writer of the Arduino code put in that const char “ message stuff instead of a simpler String message, especially since the example wrote to a text file. I think that that person wanted to generalize (always an issue with simple code!) and allow for writing into more complex file structures. (Using c in this way is like having a conversation with someone who speaks in parables and $5 words, instead of just keeping it simple !).
– You can consider this a closed issue. Thank you!