10
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 errors 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 so is there any thing like that in C to directly cast a float to a string?

5
  • 2
    Why the tag c# and c++ ?
    – Timon Post
    Dec 30, 2016 at 9:50
  • 1
    why have you tagged c# and c++ if you're using c??
    – BugFinder
    Dec 30, 2016 at 9:50
  • 1
    it was a mistake but somebody already deleted them for me , thanks .
    – Fady Sadek
    Dec 30, 2016 at 9:51
  • 1
    No this question is in c while the link you have pasted is a solution to a problem to c++
    – Fady Sadek
    Dec 30, 2016 at 10:07
  • Voting to re-open as post was mis-tagged, tags corrected, yet dupe refers to another language's answer that is irrelevant to his question. Dec 30, 2016 at 12:50

4 Answers 4

11

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);
8

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.

1
  • Suggest "%e" instead of "%f". The latter will print nearly half of all float as "0.000000' or "-0.000000'. Dec 30, 2016 at 12:37
4

By using sprintf() we can convert from float to string in c language 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");
}

Hope this will help you.

0
2
// OP's code           v---- Format string expected here. 
// fprintf(fPointer, amount);

To print different text for each float, use the format string "%.*e".
FLT_DECIMAL_DIG - 1 is the number of digits needed to print each float value uniquely without undue precision.

#include <float.h>
#include <stdio.h>

fprintf(fPointer, "%.*e", FLT_DECIMAL_DIG - 1, amount);

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.