Like Tree1Likes
  • 1 Post By grumpy

fwrite and decimal points

This is a discussion on fwrite and decimal points within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I have a program which takes the reading from a text box and saves it to a text ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    128

    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:

    Code:
    fopen_s(&pFile,"textfile.txt","w");
    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);
    ...it 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):

    3

    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?

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,662
    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++.
    Salem likes this.
    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, Sunshine, 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, 03:22 PM
  2. Decimal Points and Binary Points
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-07-2002, 01:06 AM
  3. (C++) How to detect decimal points?
    By jeffcoulter in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2002, 03:36 PM
  4. decimal points
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 04-29-2002, 11: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, 11:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21