Binary Files

This is a discussion on Binary Files within the C Programming forums, part of the General Programming Boards category; Hello all, i've got a complicated (to me) question to ask. This one's about binary files, I really don't get ...

  1. #1
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42

    Binary Files

    Hello all, i've got a complicated (to me) question to ask.

    This one's about binary files, I really don't get how they work or how to get data from them... Problem is, even if I understood the binary file, what should I get from one even if I use fread() properly, which I don't think I am.

    Sorry I really don't think posting code will be helpful since I really don't know enough about fread or binary files to really have a good example.

    Just wondering if anyone can help, and just so ya know i've done all the help yourself attempts, google had NO good results!

    Thanks for the help, I really appriciate it,
    Alex
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    http://www.antioffline.com/h/c/ch15.html

    If you have questions after reading that, come back and ask away.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ewww so much bad code in that example.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Yea, that is true. However, I felt it would be adequate to give someone an idea of how binary files work. Hopefully he won't take any of the bad habits from the example code.

    *Don't use void main as in the example.
    *Check your return values. (especially from fopen!!)

    Those are the worst things I see immediately aside from the horrible style.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Well I do have one question, you can use anything in your struct, right? Another question is why did he use xyz in the struct and only x from the struct, memory fullfillment???
    --Added--
    Also why did he use fopen("junk","r") and "w" rather than "rb" or "wb"
    Last edited by Axpen; 08-12-2004 at 02:01 PM. Reason: Added Info
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Yes, you can put anything you want in your struct. He could've used all the members or just one, it doesn't matter. Not sure why he only filled in the first one. I'm not sure of the standard default mode behavior for fopen but he should have used "rb" and "wb" to be explicit. I think on his compiler the default behavior was probably binary mode.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Also why did he use fopen("junk","r") and "w" rather than "rb" or "wb"
    Such things are only useful in a DOS/Windows environment, you don't need them in Unix/Linux
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Axpen
    This one's about binary files, I really don't get how they work or how to get data from them...
    A binary file is just another file. A text file is different.

    Let's try to make it clear.

    Basically, a file (or should we say a stream) is a sequence of octets. Nothing less, nothing more.

    What makes the difference is the semantic you give to the octets or not.

    For example, on a binary stream, each value from 0 to 255 has the same 'no-meaning'.

    Once the file has been opened in binary mode "rb" or "wb", you can use either the byte-by-byte functions fgetc() / putc() to read / write to the file or the block functions fread() / fwrite().

    The block function has an (address, size) interface to define the block to read / write.

    The data is called raw, because it is subject to no interpretation at all by the system.:

    "What you read is what you get"
    "What you write is what you have"

    The semantics of the data in the block is under te responsability of the user. He knows how is organized the stream, the size, type and meaning of the fields etc.

    Don't attempt to map a C-structure on such a bloc if you want to write portable code.

    Text files are different. They have an internal structure based on lines. The 0-255 values belong to a charset where each values has a meaning. Some are characters (For example, in ASCII, 65 is 'A'), some are control characters (13 is CR, 10 is LF etc).

    Depending on the system (yes, its a pity), the line termination sequence can be

    LF (Unix, Vax-LF)
    CR (Mac, Vax-CR)
    CRLF (MS-DOS Windows, Vax-CRLF)
    LFCR (Vax-LFCR)

    From the C point of view, there is only one character : '\n'. It meants that when I call (assuming 'stream' was opened in "w" mode):
    Code:
       fprintf (stream, "hello\n");
    it will write respectively: (hexadecimal, assuming ASCII)
    Code:
    68 65 6C 6C 6F 0A
    68 65 6C 6C 6F 0D
    68 65 6C 6C 6F 0D 0A
    68 65 6C 6C 6F 0A 0D
    and reverse (read).

    In addition, some systems have a special control character interpreted like and end of file marker.

    For example, MS-DOS has the 26 (or Ctrl-Z) character. For these reasons (interpretation) the data is called 'cooked'.
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Salem
    > Also why did he use fopen("junk","r") and "w" rather than "rb" or "wb"
    Such things are only useful in a DOS/Windows environment, you don't need them in Unix/Linux
    This is not true. It's a common mistake made by the people from the Unix world.

    The difference between text and binary is a portable C feature. It happens that in some systems, the difference between a text file and a binary file is nul. Good for them, but it's just a peculiar case. The whole world is not Unixoid. Some are MS-DOS/Windows, Mac, Vax, OS/9 etc. Not to mention the embbeded systems.

    Due to portabilty issues, the difference should be considered.
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    Adamant Programmer Axpen's Avatar
    Join Date
    Jun 2003
    Location
    USA
    Posts
    42
    Much appriciated, I understand the difference between binary and text, however, in binary all the bytes are used, such as 0x00 to 0xff, so how would one find the end of a file, since there are often 0x00 (\0) all over? For example a for loop to read until EOF?
    The Man With 3 Ears::Oh no better get the huskers

    Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

    In Case I Forget I Have:
    Windows XP
    For My 32-bit Questions:
    Dev C++ (mainly just use its mingw)
    For My 16-bit Questions:
    Borland Turbo C++ 1.01

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    EOF isn't NULL though
    Code:
    while(fgets(buffer,sizeof buffer,fp)){
    [edit]fgets() return s on success, and NULL on error or when end
    of file occurs while no characters have been read.

    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer. /*a clear difference*/
    [/edit]
    Last edited by linuxdude; 08-12-2004 at 07:22 PM.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Axpen
    Much appriciated, I understand the difference between binary and text, however, in binary all the bytes are used, such as 0x00 to 0xff, so how would one find the end of a file, since there are often 0x00 (\0) all over? For example a for loop to read until EOF?
    You could try the feof() function.

    ~/
    Last edited by kermit; 08-12-2004 at 07:55 PM.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by kermit
    You could try the feof() function.

    ~/
    Why it's bad to use feof() to control a loop

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Thantos
    edit..

    n/m
    Last edited by kermit; 08-12-2004 at 08:34 PM.

  15. #15
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Axpen
    Much appriciated, I understand the difference between binary and text, however, in binary all the bytes are used, such as 0x00 to 0xff, so how would one find the end of a file, since there are often 0x00 (\0) all over? For example a for loop to read until EOF?
    The files are managed with some internal size information that is used to detect the end of the file. The details belong to the system.

    From the C point of view, the end of file is a status that is identified by the feof() function, after a read error has been detected by one of the read functions: getc(), fscanf() and fread() returning EOF or fgets() returning NULL.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  2. send/recv binary files using sockets in C++
    By dafatdude in forum Networking/Device Communication
    Replies: 14
    Last Post: 07-25-2004, 11:00 AM
  3. MFC: CStrings & binary files question(s)
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-24-2004, 05:41 PM
  4. Binary files
    By Brian in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 12:13 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM

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