Unit16 - C++20 Format Library + Text vs. Binary Files
Unit16 - C++20 Format Library + Text vs. Binary Files
Another difference between text and binary files is that the operating system
may add a special character at the end of a text file to mark its end.
In Windows, for example, the Ctrl+Z character marks the end of a text file.
On the other hand, no character has a special significance in a binary file. They are all
treated the same.
TEXT FILES VS BINARY FILES
Storing data in a binary file can save space compared to a text file.
For example, suppose that we are using the ASCII character set to write the number
47654 in a text file. Since this number is represented with five characters, the size of
the file would be 5 bytes
On the other hand, if this number is stored in binary, the size of the file would be 2
bytes, as shown in the following figure
TEXT FILES VS BINARY FILES
Binary files might seem a better choice; they have some disadvantages.
When a binary file is transferred from one system to another the data
might not be represented the same, because different systems might
store them in different ways.
For example, a system may store first the high byte of a number, while some other
system the lower byte.
Furthermore, because the sizes of the data types may differ from system
to system, different number of bytes might be written.
For example, a function using sizeof(int) to save an integer into a file may write four
bytes in one system, eight in another.
C++ DEMO • C++ 20 <format> Library