Computer Chp 09
Computer Chp 09
Chapter 9
File Handling
Q. What is a file?
File
A file is a collection of bytes stored on a secondary storage device like hard disk. The collection of
bytes may be interpreted in different ways. For example, a text document may interpret the bytes as
characters, words, lines, paragraphs or pages. A database may interpret them as fields and records. A
graphical image may interpret them as pixels .The meaning attached to a particular file is determined
by the data structures and operations used by the program to process the file.
Types of Files
Different types of files in C++ are as follows:
1. Text Files
A type of file that stores data as readable and printable characters is called text file. A
source program of C++ is an example of text file .The user can easily view and read the contents of a
text file. It can also be printed to get a hard copy.
Text files can be a stream of characters that can be processed by a computer sequentially
in forward direction. That is why a text file is typically opened only for one type of operation at a
time such as reading or writing etc. Only one character can be read or written to the text files at a
time.
2. Binary Files
A type of file that stores data as non-readable binary code is called binary file. An
object file of a C++ program is an example of binary file. It consists of a binary code that is converted
by the compiler from source file. The user cannot read the contents of this file. It can only be read
and processed by computer.
These file can be processed using sequential or random access methods. These files can
be opened for read and write operations at the same time. For example, a database file is created
and processed as binary file. The process of updating a record will be performed as follows:
Locating the appropriate record
Reading the record into memory
Modifying the record as required
Writing the updated record back to the file
Q. What is meant by the term mode of file opening? List different modes of opening files.
Modes of File Opening
The mode of file opening indicates the type of operations to be performed
on the file after opening. A file can be opened in different modes using open() function.
Different types of modes for opening a file are as follows:
2
Mode Description
ios::in It is used to open a file for input operations.
ios::out It is used to open a file for output operations.
ios::binary It is used to open a file in binary mode.
ios::ate It is used to set the initial position at the end of the file. If this flag
is not set to any value, the initial position is the beginning of the
file.
ios::app It is used to perform all output operations at the end of file,
appending the content to current content of file. It can only be
used in the streams that are opened for output-only operations.
ios::trunk It is used to delete the previous contents of a file if the file opened
for output operations already exists.
Types of Streams
Two main types of streams in C++ are as follows:
1. Input Stream
The input stream is used to input data. It takes any sequence of bytes from an input
device such as keyboard, file or network. The act of reading data from an input stream is performed
using extraction operator >> used with an object of ifstream or fstream. The ifstream is only used
for input but fstream can be used for both input and output.
2. Output Stream
The output stream is used to output data. It takes any sequence of bytes to an
output device such as monitor, file or printer. The act of writing data to an output stream is
performed using insertion operator << with an object of ofstream and fstream. The ofstream is only
used for output but fstream can be used for both input and output.
Example
The following example will open the file example.txt in binary mode to write data at the end
of file:
ofstream Test:
Test.open ("example.txt");
Verifying File Open
The stream object returns true value if the referred fie is open. It returns false if the
file is not opened.
ofstream Test;
Test.open (“Hello.txt”);
If (Test)
Cout<<”The file is open!”:
The above example creates an ofstream obiect Test. The Open functions open the Hello.txt.
The object Test will return true if Hello.txt file is opened successfully and the message "The file is
open!" will appear on the screen.