Thread: f-write stdout

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    43

    f-write stdout

    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,798
    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.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    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().

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    How does it not work? Your own post #1 implies that it works, just that you want to do something else.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Quote Originally Posted by wiqxlzxsxj
    Seems I need another operation to do the actual conversion to a type float.
    It already is a float. What you want to do is print a textual representation of the float.

    Quote Originally Posted by wiqxlzxsxj
    I'm going to ftoa() before calling fwrite().
    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.

    Quote Originally Posted by wiqxlzxsxj
    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.
    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.

    Or perhaps you just want to do the learning exercise of writing a float to text function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    Quote Originally Posted by laserlight View Post
    It already is a float. What you want to do is print a textual representation of the float.
    Yes this is what I want.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    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

  9. #9
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    No .

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Well, if you want further help, I'd suggest that you start my answering my questions from my previous post:
    Quote Originally Posted by laserlight
    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.

    Or perhaps you just want to do the learning exercise of writing a float to text function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2016
    Posts
    43
    Quote Originally Posted by laserlight
    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.

    Or perhaps you just want to do the learning exercise of writing a float to text function?
    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.
    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?

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    888
    Quote Originally Posted by wiqxlzxsxj View Post
    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.
    I'm looking for a more direct way to just print out chars and types to the console.
    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

  13. #13
    Registered User
    Join Date
    Sep 2020
    Posts
    294
    Quote Originally Posted by flp1969 View Post
    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
    Thanks for the link - I quite often code my own conversion functions for integers, but floating point has always scared me because of complexity and corner cases.

    Seems I was right to be scared, and will continue to keep well away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lot of WRITE calls in parallel in C WRITE test code
    By Bobby1995 in forum C Programming
    Replies: 2
    Last Post: 04-28-2020, 08:47 AM
  2. write a file to stdout
    By narniat in forum C Programming
    Replies: 4
    Last Post: 11-12-2018, 10:14 AM
  3. stdout with JNI
    By GoLuM83 in forum C Programming
    Replies: 4
    Last Post: 12-14-2006, 01:27 PM
  4. write to a char* instead of stdout
    By markucd in forum C Programming
    Replies: 2
    Last Post: 08-16-2006, 02:27 PM
  5. using stdout
    By rebok in forum C Programming
    Replies: 4
    Last Post: 11-05-2004, 06:19 PM

Tags for this Thread