Thread: How do I save null characters to a file?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Question How do I save null characters to a file?

    I need to write a string that might contain null characters to a file. However, C's strings are terminated by a null character. How do I do that?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Open the file in Binary mode, and don't use string-based write routines.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    For most I/O routines, you have to indicate the number of bytes you wish to use instead of relying of the null char as an end marker (e.g. fread/fwrite() or simply read/write()).

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    2
    Thanks. I now use fputc() instead of fputs().
    And thanks for the quick responses!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 01:00 PM
  5. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM