sprintf help needed - dynamic creation

Hi, hopefully someone can point me in the right direction. I am trying to dynamically (at run time) create an sprintf command.

I have been able to dynamically create the format part, but don't know how I can dynamically create the variables part.

In the code below, I would like to create the 'stringvar1, stringvar2, floatvar1' sections dynamically at run time.

char foo[48];
String format = "%s,$s,%1.1f";
sprintf(foo, format.c_str(), stringvar1, stringvar2, floatvar1);

Note, this is only example code. My code creates the 'format' string dynamically based on configuration information from an SD card.

I have been able to dynamically create the format part, but don't know how I can dynamically create the variables part.

The format is not a String. Why on earth are you pissing away resources using a String?

char *format = "%s,$s,%1.1f";
sprintf(foo, format, stringvar1, stringvar2, floatvar1);

The last part of your statement makes no sense. When you call sprintf, you must tell it which variables to use AT THAT POINT IN TIME.

PaulS,
Thanks for your reply.

Why on earth are you pissing away resources using a String?

Maybe not everybody (ok, me) is as experienced as you. I come here asking for help and hoping to learn, not to get flamed for doing something the wrong way. I am doing it that way because I don't know how else to do it. In addition, they are my resources to piss away.

The last part of your statement makes no sense

Ok, me being not overly intelligent here, but what statement? The 'statement' in the code? The third line? What don't you understand?

There is a chance, and that would be a very high chance that I don't know what I am doing and don't know how to ask the question correctly due to my lack of experience.

May I very politely suggest that, as your reply to my post is absolutely useless to me (has not answered anything at all) that you refrain from replying unless you are actually going to answer the question, or ask for clarification so you can better understand the question?

Really sorry for my rant.

After all that, I believe I need to use vsprintf instead of sprintf.

Ok, me being not overly intelligent here, but what statement?

The part that I quoted.

After reading my code again and again to try to understand what part you don't I am still uncertain.

If I rewrite the statement to the following, does it make any more sense?

char foo[48];
String stringvar1 = "test1";
String stringvar2 = "test2";
float floatvar = 1.23456;

sprintf(foo, "%s,$s,%1.1f", stringvar1, stringvar2, floatvar1);

gjt211:
After all that, I believe I need to use vsprintf instead of sprintf.

it will hard to create va_list. I suggest to change the building of the format string to building the final string with using sprintf for one variable at a time

Hi Juraj, thanks for your input.
I have been trying to work out how to create the va_list. No good results so far.
I was starting to think if I pass in the va_list in an array it might work but have not got that far yet.
I was also thinking of building it one variable at a time but was hoping not to have to.
I might end up going that way.

gjt211:
Hi Juraj, thanks for your input.
I have been trying to work out how to create the va_list. No good results so far.
I was starting to think if I pass in the va_list in an array it might work but have not got that far yet.
I was also thinking of building it one variable at a time but was hoping not to have to.
I might end up going that way.

the va_list is to process the ... args. it is very tricky.

btw: nor do I understand what PaulS does not like on how you use sprintf in the test example

Thanks for the kind words Juraj.
I can get a vsprintf with fixed va_args to work but still gives me the same problem to solve that I started with.
In the interests of moving on, I think I will just build it one at a time as suggested so I can get on with the rest of the project.
Thanks again.

perhaps what Paul meant was that the va_list used inside variable arguments (...) functions is evaluated at compile tine

I have ended up creating my char array one value at a time but now I have a new problem.

When I send the data to my server, there is a /r appended to the end of the string (looking at it in Node-red).
My last few lines of code are;

  char rew[12];
  sprintf(rew,",10:%1.1f",therm.value);
  strcat(foo,rew);

Previously when I done it all in a single sprintf line, there was no /r at the end.
Any ideas?

gjt211:
I have ended up creating my char array one value at a time but now I have a new problem.

When I send the data to my server, there is a /r appended to the end of the string (looking at it in Node-red).
My last few lines of code are;

  char rew[12];

sprintf(rew,",10:%1.1f",therm.value);
 strcat(foo,rew);



Previously when I done it all in a single sprintf line, there was no /r at the end.
Any ideas?

is the buffer big enough for the formatted text and terminating zero?

you could use my CStringBulder class from StreamLib. search StreamLib in Library Manager.

If I rewrite the statement to the following, does it make any more sense?

It does not make sense to use String objects with Arduino. They cause memory problems and program crashes.

Use C-strings instead (character arrays) and make sure that they are declared large enough for the text and terminating zero.