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

L11 - File Input Output

Uploaded by

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

L11 - File Input Output

Uploaded by

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

CE 204: Computer Programming Sessional

File Input/Output
1
Review of Input/Output Features
• The input/output functionality provided by the
standard library in C++ involves stream objects.

• We can visualize a stream as a sequence of


information flowing between an external device
(monitor, keyboard, printer etc.) and the main
memory of the computer.

• There are two modes of data transfer: text mode


and binary mode. In addition, there are two ways
in which data can be read from or write to a
stream: formatted input/output operation and
2
unformatted input/output operation.
Review of Input/Output Features
• We have already used a standard stream object
cin for reading data and cout from writing data.
The standard streams are defined as stream class

objects within the std namespace.

cin is associated with the keyboard

cout is associated with the monitor

• The definition of the standard stream objects


appear in the header file iostream. We have seen
the use of insertion operator >> and extraction
operator <<.
3
Stream Manipulators
• We have already used stream manipulators such
as endl, setw() to control the formatting of a
stream.

• There are many other stream manipulators such


as dec, oct, scientific, left, right, setprecision()
etc.

4
Input/Output with Files
• C++ provides the following classes to perform
output and input of characters to/from files:

ofstream: Stream class to write on files

ifstream: Stream class to read from files

fstream: Stream class to both read and


write from/to files

• These classes are derived directly or indirectly


from the classes istream and ostream. We have
already used objects whose types were these
classes: cin is an object of class istream and
5
cout is an object of class ostream.
Input/Output with Files
• Therefore, we have already been using classes
that are related to our file streams. And in fact,
we can use our file streams the same way we
have already used to use cin and cout, with the
only difference that we have to associate these
streams with physical files.

6
Open a File
• The first operation generally performed on an
object of one of these classes is to associate it
to a real file. This procedure is known as to
open a file.

• An open file is represented within a program by


a stream object (an instantiation of one of these
classes) and any input or output operation
performed on this stream object will be applied
to the physical file associated to it.

7
Open a File
• In order to open a file with a stream object we
use its member function open():

open (filename, mode)

where filename is a null-terminated character


sequence of type const char * (the same type that
string literals have) representing the name of the
file to be opened, and mode is an optional
parameter.

8
Open a File
• 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:

• The default value is only applied if the function


is called without specifying any value for the
mode parameter. If the function is called with
any value in that parameter the default mode is
9
overridden, not combined.
Closing a File
• When we are finished with our input and output
operations on a file, we shall close it so that its
resources become available again. In order to
do that we have to call the stream's member
function close(). This member function takes no
parameters, and what it does is to flush the
associated buffers and close the file.

• Once this member function is called, the stream


object can be used to open another file, and the
file is available again to be opened by other
processes. 10
Open and Close File - Variation
• C++ allows the open call to be combined with
the constructor.

• For example, instead of writing:

ifstream data_file;

data_file.open("numbers.dat");

data_file.close();

• We can write:

ifstream data_file("numbers.dat");

Destructor automatically close the file when


scope ends. 11
Writing to a File
#include <iostream>

#include <fstream> // required for ofstream object

using namespace std;


This code creates a file called example.txt
int main () and inserts a sentence into it in the same
way we are used to do with cout but using
{ the file stream myfile instead.

ofstream myfile;

myfile.open ("example.txt");

myfile << "Writing this to a file.\n";

myfile.close();

} 12
Reading from a File
#include <iostream>

#include <fstream> // required for ifstream object

using namespace std;

int main ()

char data[100];

ifstream myfile;

myfile.open (“example.txt");

cout<<"Reading from a file"<<endl;


13
Reading from a File
myfile >> data;

cout<< data <<endl;

myfile.close();

This code opens a file called example.txt and


reads the contents of the file using the
stream extraction operator (>>).

Read operation continues until the first


occurrence of whitespace.
14
Reading full line from a File
#include <iostream>

#include <fstream> // required for ifstream object

using namespace std;


opens example.txt file and
int main () {
reads first line.
string line;

ifstream myfile ("example.txt");

cout<<"Reading from a file"<<endl;

getline(myfile, line);

cout<< line <<endl;

} 15
Read multi lines from a File
#include <iostream>

#include <fstream> // required for ifstream object

using namespace std;


opens example_2.txt file
int main () {
and reads all lines.
string line;

ifstream myfile ("example_2.txt");

cout<<"Reading from a file"<<endl;

while ( getline (myfile,line) ) {

cout<< line <<endl; }

} 16
Example Problem
Read a text file containing Class Test marks of
30 students. Find the average marks obtained in
that Class Test.

17
Thank You!
18

You might also like