Passing constant text to a function

I'm working on the datalogging aspect of my project.

In the text file on the SD card, I want the text to look like:

   Date   ,  Time  ,Value1,Cmd,Status
05/03/2018,13:45:34, 12.3 , On,  Run

The commas should align vertically and the values and heading centered.

My thought was to create a header write function that would accept the header text, write the text and the comma to the file, and return the column width. The column width would then be passed to the the data write function that would add the correct number of spaces before and after the data record to get the next comma in the correct location.

I can't recal; having ever written a function to receive a constant char [] value. The one I wrote is throwing an warning during the compile.

Relevant Code:

uint8_t writeHeaderText(char text[])
 {
  file.print(text);
  file.print(",");
  return(strlen(text));
 }

void logHeader()
 {
  logColWidth[0]=writeHeaderText("   Date   ");
  logColWidth[1]=writeHeaderText("  Time  ");
 }

Error:

C:\Users\Ralph-Win7\Documents\Arduino\Pool_Heater_Main_V6\Pool_Heater_Main_V6.ino: In function 'void logHeader()':

C:\Users\Ralph-Win7\Documents\Arduino\Pool_Heater_Main_V6\Pool_Heater_Main_V6.ino:503:46: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   logColWidth[0]=writeHeaderText("   Date   ");

                                              ^

C:\Users\Ralph-Win7\Documents\Arduino\Pool_Heater_Main_V6\Pool_Heater_Main_V6.ino:504:44: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

   logColWidth[1]=writeHeaderText("  Time  ");

                                            ^

I prefer my code to be warning free. Any help on how the 'correct' way to pass the text without a compiler warning. It must be doable, Serial.print("x") works just fine.

You can either cast the string to (char *) to match what the function is declared to take:

  logColWidth[0]=writeHeaderText((char *)"   Date   ");

or redefine the function itself so that it expects a const string:

uint8_t writeHeaderText(const char text[])

If you do the second one, calls to writeHeaderText will be fine with character string constants as an argument but if you pass a (char *) variable, you'll probably have to cast that. For example:

char tmp[100];
  .
  .
  strcat(tmp,"whatever");
  writeHeaderText((const char *)tmp);

I prefer my code to be warning free

A very good plan.

Pete

I thought about the second method as it just avoids the issue entirely. But that would end up using a two dimensional array. For any header that doesn't use the max width it would be wasting space. Option #1 would save space by using just the width needed.

Just use sprintf()

Mark

holmes4:
Just use sprintf()

Mark

How are you thinking to use it? The way I thought of using it took a lot programming and multi-point maintenance for changes to meet the requirements.

The way I thought of using it took a lot programming and multi-point maintenance for changes to meet the requirements.

You what!

Mark

Looks like my post got corrupted when I edited it. Instead of making it better, the website made it worse.

The way I had thought to use sprintf (a) took a lot more programming and (b) added multi-point maintenance for changes to the datalog in order to meet the requirements in the OP.

How were you thinking of using it to meet the requirements above?