2

Is there any problem with using the formatted IO operations in binary mode, especially if I'm only dealing with text files?

(1):

For binary files, reading and writing data with the extraction and insertion operators (<< and >>) and functions like getline is not efficient, since we do not need to format any data and data is likely not formatted in lines.

(2):

Normally, for binary file i/o you do not use the conventional text-oriented << and >> operators! It can be done, but that is an advanced topic.

The "advanced topic" nature is what made me question mixing these two. There is a mingw bug with the seek and tell functions which can be resolved by opening up in binary mode. Is there any issue with using << and >> in binary mode compared to text mode or must I always resort to unformatted IO if opening up in binary? As far as I can tell for text files, I just have to account for carriage-returns (\r) which aren't implictly removed/added for me, but is that all there is to account for?

Community
  • 1
  • 1
SergeantPenguin
  • 843
  • 7
  • 16
  • The *binary mode* of a file means that no translations will occur when reading or writing to the file (`ios::binary`). One common translation is line endings. This is different than using the `istream::read` and `ostream::write` to read and write unformatted data directly. – Thomas Matthews Jan 06 '16 at 19:30

1 Answers1

0

Is there any problem with using the formatted IO operations in binary mode, especially if I'm only dealing with text files?

I just have to account for carriage-returns (\r) which aren't implictly removed/added for me

If you want or need \r in your data, you are probably dealing with text / strings. For that you do not need to use binary files. Although you could open textfiles in binary mode to do a quick scan for newlines for example (line count), without having to do a less efficient readline().

Binary files are used to store binary values directly (mostly numbers or data structures), without the need to convert them to text and back to binary again.

Another advantage of binary files is that you don't have to do any parsing. You can access all your data directly, wherever it may be in the file (assuming the data is stored in a well structured manner).

For example: if you need to store records, each containing 5 32-bit numbers, you can write those directly to the binary file in their native binary format (no time wasted with converting and parsing). To later read record nr 1000 for example, you can seek directly to position 5 x 4 x (1000-1), and read your 20-byte record from there. With text files on the other hand, you would need to scan every byte from the beginning of the file, until you have counted 1000 lines (with would also be of different lengths).

You would use read() and write() (or fread() / fwrite()) directly (although << and >> could be used too for serialization of objects with variable lengths).

Binary files should also have a header with some basic information. See my answer here for more information on that.

Community
  • 1
  • 1
Danny_ds
  • 11,201
  • 1
  • 24
  • 46