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

Computer Chp 09

The document explains file handling in C++, including the definition of a file as a collection of bytes and the basic operations such as opening, reading, writing, and closing files. It describes different types of files, namely text files and binary files, and outlines various modes for opening files. Additionally, it details the concepts of input and output streams, the syntax for opening and closing files, and how to verify if a file is opened successfully.

Uploaded by

anasaziz263
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Computer Chp 09

The document explains file handling in C++, including the definition of a file as a collection of bytes and the basic operations such as opening, reading, writing, and closing files. It describes different types of files, namely text files and binary files, and outlines various modes for opening files. Additionally, it details the concepts of input and output streams, the syntax for opening and closing files, and how to verify if a file is opened successfully.

Uploaded by

anasaziz263
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1

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.

Q. What is file handling? Explain different types of files.


File Handling
File handling is the process of performing different operations on files. The basic
operations involved in file handing are as follows:
 Opening file
 Reading file
 Writing file
 Closing 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.

Q. Define stream. Describe input and output streams in detail.


Stream
A stream is a series of bytes associated with a file. It consists of data that is transferred from
one location to another. 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.

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.

Q. How is a file opened? Explain its syntax and give an example.


Opening Files
A file should be opened before it can be processed. A file pointer is declared and
associated with a file to be opened. A file can be opened by first creating 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.
Syntax
The member function open() of stream object is used to open a file as follows:
open(filename, mode);
filename It is the name of the file to be opened.
mode It is the mode in which the file is to be opened. Multiple modes can be
given using the bitwise OR operator |.
Example
The following example will open the file example.txt in binary mode to write data at the
end of file:
ofstream Test;
3

Test.open ("example.txt", ios::out | ios::app | ios::binary);


Q. What is the default file opening mode? How is file opening verified?
Default Opening Mode
The member function open() is available in all three classes ofstream, ifstream
and fstream. The function uses a default mode to open a file if mode parameter is not used by the
user. The default modes of these classes are as follows:

Class Default mode parameter


Ofstream ios::out
Ifstream ios::in
Fstream ios:in | ios::out

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.

Q. How is a file closed?


Closing a File
The opened file should be closed when the input or output operations on the file are
finished. The member function close() ofstream object is used to close a file. It takes no parameters.
Syntax
The syntax of closing a file is as follows:
file.close();
file It indicates the stream object that refers to a file
close It is the name of the function that closes the file.
Example
Test.close();
The above example uses the close() function of the stream object Test. The file associated
with stream object will be closed.

You might also like