Chapter 9
Chapter 9
Chapter No. 9
File Handling
MCQs ……………………………………………………………. Page 2
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
2
Answers
i. B ii. B iii. A iv. C v. C
vi. B vii. D viii. B ix. C x. D
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
3
Ans: - 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, paragraph or pages.
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
4
Long Questions/Answers
Ans: - File Handling: - File handling concept in C ++ is used for storing data permanently in
computer. It provides a mechanism for performing different operations on data. The basic
operations involved in file handling are as follows.
Opening file
Reading file
Writing file
Closing file
Types of files: -
C ++ divides files into the following two types based on how they store data.
Text files: - A text file in C ++ is a special kind of file. Text files can be a stream of characters that
a computer can process sequentially in forward direction. That’s why a text file is typically
opened only for one type of operation at a time such as reading, writing or appending. Text files
can read or write only one character at a time. Although in C ++, functions are available that
deal with lines of text, but these essentially process one character at a time.
Binary files: - Binary file is a collection of bytes. Binary file is also referred to as a character
stream because byte and character are equivalent in C ++ programming language. Binary files
can be processed using sequential or random access techniques depending upon the needs of
the application. Binary 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
Opening file
Reading file
Writing file
Closing file
Opening file: - A file should be opened before it can be processed. A file pointer is declared and
associated with the file to be opened. A file can be opened by first creating an object of ifstream,
ofstream or fstream. The object is then associated with a file. An open file is represented by a
stream object in a program. Any input or output operation performed on this stream object is
applied to the file associated with it. For example:
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
5
ofstream myfile;
myfile.open (“Test.bin”);
Here, myfile is an internal variable, actually an object used to handle the file Test.bin.
Reading file: - The extraction operator (>>) is used to read data from files. It can read one word
from the file at a time and store it in a variable. The extraction operator needs to be used
repeatedly to read all contents in the file.
Example: - The following piece of code declares a stream object Test of ifstream class. It reads the
contents of file in the variable ch. It will read only the first word in the line.
char ch[50[;
ifstream Test;
Test.open(“city.txt”);
Test>>ch;
Writing file: - Writing a file means to write the data into a file. The insertion operator (<<) with
an object of ofstream or fstream is used to write data into a file.
Closing file: - It is the responsibility of programmer to close the opened file when the input or
output operations on the file are finished. The close( ) function is used to close the file. It does
not require any argument.
Example: - myfile.close( );
Ans: - bof( ) function: - bof( ) stands for beginning-of-file. The bof( ) is a pointer which returns true if
the current position of the pointer is at the beginning of the input file stream and false otherwise. It
means that bof( ) tells the compiler whether the cursor is at the beginning of file or not. For example
myfile.close( );
eof( ) function: - eof( ) stands for end-of-file. The eof( ) is a pointer which returns true when there
are no more data to be read from an input file stream and false otherwise. It means that this
function checks whether control has reached to the end of file or not. This function is very useful in
the case when we do not know the exact number of records in a file.
For example:
while( !flag.eof( ))
{
flag>>ch;
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
6
cout<<ch<<endl;
flag.close( );
Ans: - Stream: - In C ++ stream is a sequence of bytes associated with a file. Most of the times
streams are used to assure good and secure flow of data between an application and file. Each
stream is associated with a specific file by using the open operation. The information can be
exchanged between a file and the program once a file is opened.
In C ++ input stream and output stream are the two types of streams.
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
by using extraction operator (>>) with an object of ifstream or fstream. The ifstream is only used for
input but fstream can be used for both input and output.
Output stream: - The output stream is used to output data. It sends 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 by using insertion operator (<<) with an object of ofstream or fstream. The ofstream is
only used for output but fstream can be used for both input and output.
5. What is meant by the term mode of file opening? Describe different modes of opening file.
Ans: - Mode of file opening: - The mode of file opening indicates the type of operations to be
performed on the file after opening. In order to open a file in any desired mode, the member
function open( ) should take mode as an argument along with the file name. Its general syntax is:
Here, file_name representing the name of the file to be opened and mode is an optional parameter
with a combination of the following flags.
Mode Description
ios::in Open for input operations (Reading a file)
ios::out Open for output operations (Writing a file)
ios::binary Open in binary mode.
ios::ate Open a file for output and move the read/write control to the end of the file.
ios::app Append mode. All output to that file to be appended to the end.
ios::trun If the file is opened for output operations, its existing contents are deleted
and replaced by the new one.
Each one of the open( ) member functions of the classes ofstream, ifstream and fstream has a
default mode that is used if the file is opened without a second argument.
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
7
Ans: - The data can be read from and written to files with the help of single character stream and
string stream.
a) Single character stream: - Using single character stream, the data can be read from and written
to files character by character.
i. Reading files character by character: - The function get( ) is used to read data character by
character from files.
Example: - The following program reads the data one character at a time from the file
“charactersfile.txt” and displays them on the screen.
Output:-
# include <iostream.h>
R
# include <conio.h>
e
# include <fstream.h>
a
main ()
{ d
clrscr ( ); t
char ch; h
ifstream reads (“c:\\charactersfile.txt”); e
while (!reads.eof( ))
f
{
i
reads.get(ch );
l
cout<<ch<< endl;
} e
reads.close( ); p
getch ( ); l
} e
a
s
e
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
8
ii. Writing files character by character: - The function put( ) is used to write data character by
character to files.
# include <iostream.h>
# include <conio.h> Output:-
# include <fstream.h>
C ++ is a powerful language.
main ()
{
clrscr ( );
char ch;
ofstream writes (“d:\\characterswrite.txt”);
for (int I = 0; i<=30; i++)
{
cin>>ch;
cout <<writes.put(ch);
}
writes.close( );
getch ( );
}
b) String stream: - A string stream is a stream which reads input from or writes output to an
associated string.
Example: - The following program reads “strinread.txt” file into string str using getline ( ) function
and display the result on the screen.
# include <iostream.h>
# include <conio.h> Output:-
# include <fstream.h>
This is the end of C ++ long questions.
# include <string.h>
main ()
{
clrscr ( );
char str[40];
ifstream reads (“d:\\stringread.txt”);
while (!reads.eof( ))
{
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar
9
Second Year Computer Notes By Riaz Khattak APS & C (Boys) Peshawar