I have this little program. It outputs the binary conversion to the console (aka garbled output). Is there a way to fwrite() numerical types to the output?
Code:#include int main() { float f = 0.1; fwrite(&f, sizeof f, 1, stdout); return 0; }
I have this little program. It outputs the binary conversion to the console (aka garbled output). Is there a way to fwrite() numerical types to the output?
Code:#include int main() { float f = 0.1; fwrite(&f, sizeof f, 1, stdout); return 0; }
Yes, you use fprintf() instead.
Or you sprintf it to a string, then fwrite the string.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I was looking to not use printf family of functions.
Curious though why doesn't fwrite() work in this example? This is what a few tutorials show how to use fwrite().
How does it not work? Your own post #1 implies that it works, just that you want to do something else.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What is fwrite() doing? It's outputting the contents at &f in its binary form to stdout. Seems I need another operation to do the actual conversion to a type float. My bad...
Now it gets worse. I'm going to ftoa() before calling fwrite().
I wonder if you guys have any qualitative insight into representing numeric data in human readable form this way over printf. printf is certainly an easier and legible way to do it but I have the flexibility to omit conveniences for the timebeing.
It already is a float. What you want to do is print a textual representation of the float.Originally Posted by wiqxlzxsxj
What's the point? ftoa is a non-standard function, whereas you have the family of printf/sprintf functions that are already part of the standard library.Originally Posted by wiqxlzxsxj
Is there something about printf that isn't doing the job the way you want it? You need to figure that part out before you try building something better, otherwise there's a pretty good chance you'll waste your time building what has already been done, or maybe even end up with something worse.Originally Posted by wiqxlzxsxj
Or perhaps you just want to do the learning exercise of writing a float to text function?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
1) Declare a char array
2) Using the function "sprintf", put the float into a string format and in the char array
3) Print the char array to the screen
Here is a quick overview of sprintf and the example at the bottom will help you a lot - C library function - sprintf() - Tutorialspoint
Fact - Beethoven wrote his first symphony in C
No .
Well, if you want further help, I'd suggest that you start my answering my questions from my previous post:
Originally Posted by laserlight
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The printf family of functions work perfectly in my example and are legible to read. They are "the standard" way to implement io/manipulate strings.Originally Posted by laserlight
I'm looking for a more direct way to just print out chars and types to the console.
See here's the problem, functions like printf are taken for granted and hide away all the underlying mechanics. I don't really know or understand how these functions actually work.
When someone comes in with a printf solution that's not addressing the real problem.
I'm well aware by trying to build something better, this results in something worse, usually. I'm a strong proponent of KISS.
printf works by format specifiers which I don't need. I need to print just two or three char[] and numerical types at a time.
So my question is, I'm looking for a barebones way of getting things printed to the console.
Let's think asm for a moment. Can I get some explanation of using C instead to solve such a problem?
Then, I suggest you start to study how to print floating point values from scratch. It's not a simple task:
How to print floating point numbers accurately