• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

generating const char * message for writing to a file in SIPFFS

Q&A Forum › Category: ESP32 › generating const char * message for writing to a file in SIPFFS
0 Vote Up Vote Down
Joe Margevicius asked 3 years ago

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.

4 Answers
0 Vote Up Vote Down
Joe Margevicius answered 3 years ago

… 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.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

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
 
 

0 Vote Up Vote Down
Joe Margevicius answered 3 years ago

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!

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.