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.
Printable View
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.
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
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. :)
>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
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
>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
As always, your response was informative and 'enlightening'. :)
Thanks for your time.
-Skipper