Thread: Opening files in binary mode

  1. #1
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Opening files in binary mode

    I just what to ask what's the point from opening files in binary mode, and what are the uses for it.

    I found tutorials about "How to open files in binary mode", but it's not what I want.

    Thanks for help.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Data stored in files in binary mode are written to the files as they would be stored in the computer rather than as "strings".

    For example, ints require two bytes in binary rather than five bytes if written as text like this, "12345". Gets worse, of course, with larger values.

    Opening the file in binary mode is necessary if the file was written in binary mode. Short answer, but not much more to it than that.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Is it possible to write a file in other then binary and "strings". Such as hex or anything else. I know it wouldn't make much sense but I couldn't help asking.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I just what to ask what's the point from opening files in binary mode
    It can be more efficient both with speed and space usage. You can store values directly instead of having to write them as strings of characters and convert them back when reading from the file as well. There are many uses, but the general consensus is to use text when possible and binary when necessary because binary files effectively kill portability.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Okay, Prelude, now you've got me.

    I would have surmised that portability would be increased. ("Surmised" being closely likened to "guessed" except that it reads slightly better.)

    Are we back to "implementation-defined" where portability is concerned?

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Are we back to "implementation-defined" where portability is concerned?
    Something like that.

    The layout of a binary file is a direct mapping of the data, it depends an many different platform specific details. The number of bytes, byte order, and representation are all factors. And that is just for integer values, floating point values are even worse, the same size and byte order factors exist, but floating-point values vary in both the number of bits for the mantissa as well as the exponent and the data may not even be in base ten. For structures, you have to consider the platform dependent alignment and padding of the data.

    So you can't be sure that a binary file can be transferred to another compiler, much less another machine. Now, you can read and write the binary data manually, but that is usually more effort than it's worth, so text files are preferred.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    As always, your response was informative and 'enlightening'.

    Thanks for your time.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying Binary Files
    By mikeman118 in forum C++ Programming
    Replies: 9
    Last Post: 08-11-2007, 10:55 PM
  2. Binary files
    By Lionmane in forum C Programming
    Replies: 35
    Last Post: 08-25-2005, 01:56 PM
  3. Reading Binary files
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2005, 06:48 AM
  4. fstream binary files
    By wesdgreat in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 10:12 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM