Thread: How to write human readable text to a file? C programming.

  1. #1
    Registered User
    Join Date
    Feb 2015

    How to write human readable text to a file? C programming.

    Hi. I don't know why I am so confused by this, it should be simple.

    I want to be able to write text to a file that would be recognized by humans. I have a small program that writes a floating point value to a file. However, the text file that is created has in it something that is not in human readable form. Does this mean that I can only write values of type "char" to a file so that they are human readable? If that is the case then should I typecast this value before writing it to the file?

    float rval = 4.7;
    FILE *fptr;
    fptr = fopen("text.txt", "w");

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    You need fprintf rather than fwrite. fwrite writes in bytes where as fprintf writes in string.

    fprintf(fptr, "%f", rval);
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Jun 2011
    It is also a good idea to check return values, especially to make sure that the file opened successfully before trying to access it.

  4. #4
    Registered User
    Join Date
    Feb 2015
    Thank You!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Human-readable data storage format - tips?
    By Elysia in forum Tech Board
    Replies: 7
    Last Post: 01-18-2014, 01:57 AM
  2. Replies: 2
    Last Post: 07-18-2009, 01:17 PM
  3. Write to a Text File
    By slowcoder in forum C Programming
    Replies: 3
    Last Post: 08-14-2007, 07:52 AM
  4. write a file text
    By mickey0 in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2007, 03:36 AM
  5. Write some text to a file
    By Unlimited4s in forum C++ Programming
    Replies: 7
    Last Post: 07-17-2002, 03:37 AM