0% found this document useful (0 votes)
32 views

Unit16 - C++20 Format Library + Text vs. Binary Files

Uploaded by

cdw7rk4f5b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Unit16 - C++20 Format Library + Text vs. Binary Files

Uploaded by

cdw7rk4f5b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CSE142

OBJECT ORIENTED PROGRAMMING


TECHNIQUES
Fall 2024
Unit#16
Compiled by Solat Jabeen
Content is taken from the reference books and lecture slides
provided with ‘Starting Out with C++ Early Objects Seventh
Edition’
C++20 STD::FORMAT LIBRARY
The std::format is a text formatting library since C++20, which
offers a safe and extensible alternative to the printf family of
functions.
It is intended to complement the existing C++ I/O streams library
and reuse some of its infrastructure such as overloaded insertion
operators for user-defined types.
std::format supports a wide range of formatting options, including
the ability to format user-defined types. It is more efficient than
previous string formatting options in C++, such as sprintf and
printf.
TEXT FILES VS BINARY FILES
A text file consists of one or more lines that contain readable characters
according to a standard format, like the ASCII code.
 The text file is very easy to read and write as we can use any available editor to
process it.
 Each line ends with the special character(s) the operating system uses to indicate the
end of line.
 In Windows systems, for example, the pair of '\r' (Carriage Return) and '\n' (Line
Feed) characters, that is, with ASCII codes 13 and 10 respectively, indicate the end
of line.
 Therefore, the new line character '\n' is replaced by '\r' and '\n' when written in the
text file. The reverse replacement happens when the file is read. On the other hand,
this replacement does not take place in Unix systems because the '\n' character
indicates the end of line.
TEXT FILES VS BINARY FILES
Unlike the text files, the bytes of a binary file don’t necessarily represent
readable characters.
 For example, an executable C++ program, an image or a sound file is stored in binary. If
you open it, you’ll probably see some unintelligible characters.
 A binary file is not divided into lines and no character translation takes place. In Windows,
for example, the new line character is not expanded to \r\n when written in the binary file

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

You might also like