K
krister
Hello,
I'm working in a quite large system that has some limitations. One of
those is that I can't use printf() to get an output on a screen. I'm
forced to use a special function, let's call it PrintOnConsole(), to get
the output on a console. The problem with PrintOnConsole() is that it
only takes strings as input arguments. On the other hand, I'm free to
use sprintf(), so I can convert everything I want to print into a string
and then forward that string to PrintOnConsole().
My problem now is that I don't know how many characters sprintf() will
put in the buffer provided as first input argument.
Here is a code example:
/*-------------------- Begin --------------------------*/
#include <stdio.h>
/* This is just a dummy function, which I don't have access to in reality */
float GimmeAFloat(void)
{
return 34.2;
}
/* This is just a dummy function, which I don't have access to in reality */
void PrintOnConsole(char *string)
{
/* printf() is used here only for testing purposes. It’s not
available in reality in my system */
printf("%s", string);
}
int main(void)
{
float value;
char buf[25];
value = GimmeAFloat();
/* Pre check wanted here. If the value is too long I want to copy the
string “The value is too long\n” to buf instead. */
sprintf(buf, "This is my value: %f\n", value);
PrintOnConsole(buf);
return 0;
}
/*--------------------- End -------------------------*/
Is there any elegant way to do some kind of pre-check how many
characters sprintf() would need?
I’ve used float in this example, but the question is more general. I’d
like to be able to print int, double, long, etc.
I know that sprintf() returns the number of characters printed, but
that’s too late (I’ve already called sprintf() by then).
(It would be nice if it was possible to call sprintf() with NULL as
first argument just to investigate the return value.)
A bonus question: In the code above everything looks OK for me, the
string provided to sprintf() is 18 characters long plus 4 characters for
the float value plus 1 linefeed character plus the terminating null
character makes all in all 24 characters. The output, however, shows:
This is my value: 34.200001
Why isn’t
This is my value: 34.2
printed?
Thanks in advance,
Krister
I'm working in a quite large system that has some limitations. One of
those is that I can't use printf() to get an output on a screen. I'm
forced to use a special function, let's call it PrintOnConsole(), to get
the output on a console. The problem with PrintOnConsole() is that it
only takes strings as input arguments. On the other hand, I'm free to
use sprintf(), so I can convert everything I want to print into a string
and then forward that string to PrintOnConsole().
My problem now is that I don't know how many characters sprintf() will
put in the buffer provided as first input argument.
Here is a code example:
/*-------------------- Begin --------------------------*/
#include <stdio.h>
/* This is just a dummy function, which I don't have access to in reality */
float GimmeAFloat(void)
{
return 34.2;
}
/* This is just a dummy function, which I don't have access to in reality */
void PrintOnConsole(char *string)
{
/* printf() is used here only for testing purposes. It’s not
available in reality in my system */
printf("%s", string);
}
int main(void)
{
float value;
char buf[25];
value = GimmeAFloat();
/* Pre check wanted here. If the value is too long I want to copy the
string “The value is too long\n” to buf instead. */
sprintf(buf, "This is my value: %f\n", value);
PrintOnConsole(buf);
return 0;
}
/*--------------------- End -------------------------*/
Is there any elegant way to do some kind of pre-check how many
characters sprintf() would need?
I’ve used float in this example, but the question is more general. I’d
like to be able to print int, double, long, etc.
I know that sprintf() returns the number of characters printed, but
that’s too late (I’ve already called sprintf() by then).
(It would be nice if it was possible to call sprintf() with NULL as
first argument just to investigate the return value.)
A bonus question: In the code above everything looks OK for me, the
string provided to sprintf() is 18 characters long plus 4 characters for
the float value plus 1 linefeed character plus the terminating null
character makes all in all 24 characters. The output, however, shows:
This is my value: 34.200001
Why isn’t
This is my value: 34.2
printed?
Thanks in advance,
Krister