Thread: fwrite and decimal points

  1. #1
    Registered User
    Join Date
    Feb 2010

    fwrite and decimal points

    Hi everyone,

    I have a program which takes the reading from a text box and saves it to a text file using the following code:

    fwrite (buf , 1 , sizeof(buf) , pFile ); //pFile and buf have been declared earlier (buf is the variable storing the user's unput to the text box).
    fclose (pFile); seems to work OK when I put non-integers into buf, but when buf = an integer, the text file doesn't just have the number stored in it - it has this instead (for example when I put '3' into buf):


    Why does it add that strange character on the end, and how can I stop it from doing that? Failing that, is there a way to always add a decimal point to a certain number of decimal places onto the end?


  2. #2
    Registered User
    Join Date
    Jun 2005
    fwrite() is binary output. The output you are seeing therefore represents how an int with the value 3 is represented in computer memory. If you want text (human readable) output, use a function liike fprintf() to product your output.

    Incidentally, fopen_s() is not a standard way of opening a file. Apart from that, your code is idiomatic C, not C++.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dealing with decimal points
    By dre in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2009, 02:22 PM
  2. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of
    Replies: 9
    Last Post: 11-07-2002, 12:06 AM
  3. (C++) How to detect decimal points?
    By jeffcoulter in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2002, 02:36 PM
  4. decimal points
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 04-29-2002, 10:01 PM
  5. How To Round A Float To Two Decimal Points?
    By shahid in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2001, 10:24 AM
Website Security Test