You are on page 1of 16

17.1.

1 Difference Between Binary and Text


File in C++ Programming
Text File Binary File
• The text files can easily be • Binary files cannot easily be
transferred from one computer transferred from one computer
system to another.
• It stores data in binary format
• It stores data using ASCII i.e. with the help of 0 and 1.
format i.e. human-readable
graphic characters.
By: Najeeb Ather
17.1.1 Difference Between Binary and Text
File in C++ Programming
Text File Binary File
• These files are easily readable • These files are not easily
and modifiable because the readable and modifiable
content written in text files is because the content written in
human readable. binary files is not
human-readable and it is
• These files create portability
encrypted content.
problems.
By: Najeeb Ather • These files are easily portable.
17.1.1 Difference Between Binary and Text
File in C++ Programming
Text File Binary File
• Error in a textual file can be • Error in a binary file corrupts
easily recognized and the file and is not easily
eliminated. detected.
• In a text file, a special • There is no such special
character with ASCII code 26 is character in the binary file to
inserted at the end of the file. signal EOF.
This character signals the EOF
to
By:the program
Najeeb Ather when
encountered.
17.1.1 Difference Between Binary and Text
File in C++ Programming
Text File Binary File
• Text files are used to store data • Binary files are used to store
more user friendly. data more compactly.
• Mostly .txt and .rtf are used as • Can have any application
extensions to text files. defined extension.

By: Najeeb Ather


17.1.2 Compare Different Modes of Opening
A File

By: Najeeb Ather


17.1.3 Write Program For File Opening and
Closing
• Before opening and closing of the file in C++ we should know that which header files
and built-in classes are required to accomplish this task.
• There is only one header file that is required when you are doing file handling and that
is “fstream” e.g #include <fstream>
• There are also three classes which are used for reading, writing and for both read and
writing
• ifstream: It is used for open to read only
• ofstream: Output to the file on the disk for writing
• fstream: It is used for both reading and writing purpose
By: Najeeb Ather
17.1.3 Write Program For File Opening and
Closing
• Before opening and closing of the file in C++ we should know that which header files
and built-in classes are required to accomplish this task.
• There is only one header file is required when you are doing file handling and that is
“fstream” e.g #include <fstream>
• There are also three classes which are used for reading, writing and for both read and
writing
• ifstream: It is used for open to read only
• ofstream: Output to the file on the disk for writing
• fstream: It is used for both reading and writing purpose
By: Najeeb Ather
17.1.3 Write Program For File Opening and
Closing

By: Najeeb Ather


17.1.3 Write Program For File Opening and
Closing

By: Najeeb Ather


17.1.3 Write Program For File Opening and
Closing
• Closing file is necessary for the following reasons:
• Release system resources
• Make the file available for other programs that might need to write it.

By: Najeeb Ather


17.1.4 Write a C++ program to read a file
character by character and show output

• There are two function for reading and writing on the file they are:
• get() : A function used for reading character from a file
• put(): A function used for writing character to a file

By: Najeeb Ather


17.1.4 Write a C++ program to read a file
character by character and show output

By: Najeeb Ather


17.1.4 Write a C++ program to read a file
character by character and show output

By: Najeeb Ather


17.1.5 Write a C++ program to read a file
character by character and show output

By: Najeeb Ather


17.1.6 Write a C++ program to read a file Into
String

By: Najeeb Ather


17.1.6 Write a C++ program to write a file Into
String

By: Najeeb Ather

You might also like