Reputation: 1140
FILE * fPointer;
float amount = 3.1415;
fPointer = fopen("vending.txt", "w");
fprintf(fPointer, amount);
printf("The file has been created for the first time and "
"we added the value %f", amount);
fclose(fPointer);
I am trying to save a float number to a text file, but when I try to run this code it triggers a compiling error because the function fprintf
expects the second parameter to be an array of characters.
So how can I convert my float to a string so I can pass it? I have a C# background where something like .toString()
is possible, is there anything like that in C to directly convert a float to a string?
Upvotes: 11
Views: 112472
Reputation: 153517
to save a float number to a text file
Use "%e"
, "%g"
, "%a"
To print unique text for each float
, use the format string "%.*e"
, "%.*g"
or "%a"
with sufficient precision.
FLT_DECIMAL_DIG - 1
is the number of digits needed with "%.*e"
(FLT_DECIMAL_DIG
is the number of digits needed ,with "%.*g"
)
to print each float
value uniquely without undue precision.
#include <float.h>
#include <stdio.h>
fprintf(fPointer, "%.*e", FLT_DECIMAL_DIG - 1, amount);
// or
fprintf(fPointer, "%.*g", FLT_DECIMAL_DIG, amount);
// or
fprintf(fPointer, "%a", amount); // Hexadecimal significand
What is nice about this is that the upper bound of characters needed can be pre-calculated if one wants to print to a string (typically about 18).
// - d . dddddddd e - expo \0
#defined FLT_STR_SIZE (1 + 1 + 1 + (FLT_DECIMAL_DIG - 1) + 1 + 1 + 4 + 1
char buffer[FLT_STR_SIZE];
snprintf(buffer, sizeof buffer, "%.*e", FLT_DECIMAL_DIG - 1, amount);
"%f"
weaknesses
Using "%f"
will print nearly half of small float
like 0.000000001f
and -1.2345e-10
as 0.000000
or -0.000000
.
Using "%f"
will print large float
like FLT_MAX
with verbose text like "340282346638528859811704183484516925440.000000".
OP's coding error's include:
// OP's code v---- Format string expected here.
// fprintf(fPointer, amount);
Upvotes: 3
Reputation: 89
By using sprintf()
we can convert from float to string in C. For better understanding see the below code:
#include <stdio.h>
int main()
{
float f = 1.123456789;
char c[50]; //size of the number
sprintf(c, "%g", f);
printf(c);
printf("\n");
}
Upvotes: 3
Reputation: 4462
If you can use C99 standard, then the best way is to use snprintf
function. On first call you can pass it a zero-length (null) buffer and it will then return the length required to convert the floating-point value into a string. Then allocate the required memory according to what it returned and then convert safely.
This addresses the problem with sprintf that were discussed here.
Example:
int len = snprintf(NULL, 0, "%f", amount);
char *result = malloc(len + 1);
snprintf(result, len + 1, "%f", amount);
// do stuff with result
free(result);
Upvotes: 15
Reputation: 43886
The second parameter is the format string after which the format arguments follow:
fprintf(fPointer, "%f", amount);
%f
tells fprintf
to write this argument (amount
) as string representation of the float value.
A list of possible format specifiers can (for example) be found here.
Upvotes: 8