File read and write question - difference between zero and null ????

This is a discussion on File read and write question - difference between zero and null ???? within the C Programming forums, part of the General Programming Boards category; Hi ! I ran into a few problems while reading and writing some data to a file and here are ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    File read and write question - difference between zero and null ????

    Hi !

    I ran into a few problems while reading and writing some data to a file and here are the questions that popped up :

    1. when I write a structure member which is equal to zero into a file using fwrite (file opened for writing in binary mode) does that compiler write it as null or zero ?

    2. If the above wrriten value is written as zero and when i open the file for reading using fread (file opened in binary mode) does the compiler read it as a integer zero or a NULL ?

    any help would be appreciated !!!!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    NULL is 0.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    fwrite() will throw an ASCII value 0 into the file. If the struct member is a char then it will write 1 byte of 0's, if it's a 32-bit int it will write 4 bytes of 0's to the file, etc. Of course, if you open the file in a text editor you won't see a 0 since ASCII value 0 and the character '0' are entirely different beasts.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I've done some research and here's what I've found about NULL:
    "NULL is often used to indicate that a pointer points to nothing. It is simply the value 0 (either as an int or as a long). Note that in C, NULL often is defined as (void*)0. This is incorrect in C++ because there the type of NULL must be an integer type. Otherwise, you could not assign NULL to a pointer. This is because in C++ there is no automatic conversion from void* to any other type "

    I know this is a C board but I think someone will find this helpfull

    Micko

Popular pages Recent additions subscribe to a feed

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