C++ Files and Streams

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

C++ Files and Streams

Mujeeb Rahman K
Asstnt Professor
MES KVM College Valanchery
Why Use Files?
 All the programs we have looked at so far use input only from the
keyboard, and output only to the screen.
 If we were restricted to use only the keyboard and screen as input
and output devices, it would be difficult to handle large amounts
of input data, and output data would always be lost as soon as we
turned the computer off.
 To avoid these problems, we can store data in some secondary
storage device, usually magnetic tapes or discs.
 Data can be created by one program, stored on these devices, and
then accessed or modified by other programs when necessary.
 To achieve this, the data is packaged up on the storage devices as
data structures called files.
 The easiest way to think about a file is as a linear
sequence of characters.
 In a simplifed picture (which ignores special
characters for text formatting) these lecture notes
might be stored in a file called "Lecture_4" as:
Streams
 We can think of a stream as a channel or conduit on
which data is passed from senders to receivers.
 As far as the programs we will use are concerned,
streams allow travel in only one direction.
 Data can be sent out from the program on an output
stream, or received into the program on an input stream.
 For example, at the start of a program, the standard
input stream "cin" is connected to the keyboard and the
standard output stream "cout" is connected to the
screen.
 So far, we have been using the iostream standard
library, which provides cinand cout methods for
reading from standard input and writing to
standard output respectively.
 how to read and write from a file?
 This requires another standard C++ library
called fstream, which defines three new data
types:
To perform file processing in C++, header files <iostream> and <fstream> must be
included in your C++ source file.
Opening a File:

 A file must be opened before you can read from it


or write to it.
 Either the ofstream or fstream object may be
used to open a file for writing and
 ifstream object is used to open a file for reading
purpose only.
 following is the standard syntax for open() function,
which is a member of fstream, ifstream, and
ofstream objects.

void open(const char *filename, ios::openmode mode);

 Here, the first argument specifies the name and location of the file to be
opened and the second argument of the open() member function defines the
mode in which the file should be opened.
 You can combine two or more of these values
by OR ing them together.
 For example if you want to open a file in write
mode and want to truncate it in case it already
exists, following will be the syntax:
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
 Similar way, you can open a file for reading and writing purpose as
follows:
 fstream afile;
 afile.open("file.dat", ios::out | ios::in );
Closing a File

 When a C++ program terminates it automatically


closes flushes all the streams, release all the
allocated memory and close all the opened files.
 But it is always a good practice that a
programmer should close all the opened files
before program termination.
 Following is the standard syntax for close()
function, which is a member of fstream, ifstream,
and ofstream objects.
void close();
Writing to a File:

 While doing C++ programming, you write


information to a file from your program using the
stream insertion operator (<<) just as you use that
operator to output information to the screen.
 The only difference is that you use an
ofstream or fstream object instead of
the cout object.
Reading from a File:

 You read information from a file into your


program using the stream extraction operator
(>>) just as you use that operator to input
information from the keyboard.
 The only difference is that you use
an ifstream or fstream object instead of
the cin object.
 Read & Write Example:
 Following is the C++ program which opens a file
in reading and writing mode.
 After writing information inputted by the user to
a file named afile.dat, the program reads
information from the file and outputs it onto the
screen:
When the above code is compiled and executed, it produces the following sample
input and output:
 To connect the ifstream "in_stream" to the file
"Lecture_4", we use the following statement:
in_stream.open("Lecture_4");
 This connects "in_stream" to the beginning of
"Lecture_4".
 Diagramatically, we end up in the following
situation:
 To connect the ofstream "out_stream" to the file
"Lecture_4", we use an analogous statement:
out_stream.open("Lecture_4");
 Although this connects "out_stream" to
"Lecture_4", it also deletes the previous contents
of the file, ready for new input.
 Diagramatically, we end up as follows:
 To disconnect connect the ifstream "in_stream" to
whatever file it is connected to, we write:
in_stream.close();
Diagramatically, the situation changes from that of
Figure 4.2.1 to:
 The statement:
out_stream.close();
 has a similar effect, but in addition the system
will "clean up" by adding an
 "end-of-file" marker at the end of the file.
 Thus, if no data has been output to "Lecture_4"
since "out_stream" was connected to it, we
change from the situation in Figure 4.2.2 to:
Input using "get(...)"
 Having opened an input file, we can extract or read single characters
from it using the member function "get(...)".
 This function takes a single argument of type "char".
 If the program is in the state represented in Figure 4.2.1, the statement
 in_stream.get(ch);
 has two effects:
 (i) the variable "ch" is assigned the value "'4'", and
 (ii) the ifstream "in_stream" is re- positioned so as to be ready to
input the next character in the file.
 Diagramatically, the new situation is:
Output using "put(...)"

 We can input or write single characters to a file


opened via an ofstream using the member
function "put(...)".
 Again, this function takes a single argument of
type "char".
 If the program is in the state represented in 
Figure 4.2.2, the statement
out_stream.put('4');
 changes the state to:

You might also like