How do I save null characters to a file?

This is a discussion on How do I save null characters to a file? within the C Programming forums, part of the General Programming Boards category; I need to write a string that might contain null characters to a file. However, C's strings are terminated by ...

  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
    Katy, Texas
    Posts
    2,309
    Open the file in Binary mode, and don't use string-based write routines.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    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, 05:23 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  4. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 12: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

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