You are on page 1of 21

BITG 1113:

Files : Input Output

LECTURE 10

1
Objectives:

• To understand the fundamentals of files


and streams – input/output
• Able to write programs that use data files
for input and output.

2
FILES

• File – an external collection of related data treated as a


unit.
• Purpose – to keep a record of data.

record of data
3
FILES

• Why use files?


– In normal way, it is difficult to handle large amounts of
input data and output data. The data would be lost as
soon as we turned the computer off.
– By using files, data can be stored in a more
permanent form.
– Data can be created by one program, stored on
secondary devices, and then accessed or modified by
other programs when necessary.

4
FILES

• Two major classes of files:


– Text files
– Binary files
• Files are stored in secondary storage devices. Two of its
most common forms are:
– Disk (Hard disk, CD, and DVD)
– Tape

5
FILES & STREAM

• Files in a secondary storage can be both read and


written. These processes are also known as I/O or
input/output.

• Files exist separately from the computer and our


programs, so we must be able to create a linkage
between the external file and its usage in our program.

• A stream is this linkage. Stream is an abstract


representation of input data source, or output data
destination, used in our program.

6
FILES & STREAMS

• To use files, input streams and/or output streams have to


be created depending on the required file operations.

• Stream classes have been included in a <fstream>


header file. This header file also automatically includes
the <iostream> header file, so we no longer need to
include it.

• The type of stream class determines if it is an input file or


an output file or both.

7
FILES & STREAMS

• Input file are of type ifstream (Stream class to read from


files).

• Output files are of type ofstream (Stream class to write


on files).

• Input & output files are of type fstream (Stream class to


both read and write from/to files).

8
FILES & STREAMS

• We can use our file streams the same way we use cin and
cout, with the only difference that we have to associate
these streams with physical files.

// basic file operations


#include <iostream>
#include <fstream>
using namespace std;
int main ()
{ ofstream myfile("example.txt");//open file example.txt and
associate output stream object myfile with it.
myfile << "Writing this to file example.txt.\n"; //write output to
file
cout << "Writing this to the monitor.\n"; //write output to monitor
myfile.close(); //close file example.txt
return 0;
}

9
FILES & STREAMS

• This code creates a file called example.txt and inserts a


sentence into it in the same way we are used to do with
cout, but using the stream object myfile instead.

• The content of file example.txt:


Writing this to file example.txt.

• The output displayed on the computer’s monitor is:


Writing this to the monitor.

10
INPUT/OUTPUT FILES

• A program that prints to an output file OR reads data


from an input file should make sure the file :

1. Has an acceptable file name.


2. Is opened before it is used.
3. Is closed after it is used.

11
INPUT/OUTPUT FILES : Opening a file

• There are a few modes to open a file. They are listed as


follows:
Mode Description
ios::in - Open a file for input operations.
ios::out - Open a file for output operations.
ios::ate - Open a file for output operations. Set the initial
position at the end of the file.
ios::app - Open a file for output operations. All output
operations are performed at the end of the file,
appending the content to the current content of the
file.
ios::trunc - If the file opened for output operations already
existed before, its previous content is deleted and
replaced by the new one.

12
INPUT/OUTPUT FILES : Opening a file

• All these flags can be combined using the bitwise operator


OR (|).

• For example, if we want to open the file example.bin to


add data we could do it by the following :

ofstream myfile("example.bin", ios::out | ios::app);

13
INPUT/OUTPUT FILES : Opening a file

• For all stream classes, default mode/s is/are


automatically assumed, even if a mode that does not
include them is passed as second argument to open the
file.

• Stream class Default mode parameter


ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out

• The default value is only applied if the file is opened


without specifying any value for the mode parameter.

14
INPUT/OUTPUT FILES : 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 function


close(). This function takes no parameters, and what it
does is to flush the associated buffers and close the file:
myfile.close();

• Once this member function is called, the stream object


(above eg: myfile) can be used again to open another
file.

15
OUTPUT FILES

• Writing output to a text file : Example 1

# include <iostream>
# include <fstream>
using namespace std;

int main()
{
double income = 123.45, expenses = 987.65;
int week = 7, year = 2006;

ofstream outfile("L4_2.out");// default mode is ios::out


outfile<<"Week = "<<week<<endl<<"Year = "<<year<<endl;
outfile<<"Income = "<<income<<endl<<"Expenses = "<<expenses<<endl;
outfile.close();
return 0;
}

16
OUTPUT FILES

• Content of L4_2.out text file :

• You can find the L4_2.out file in the same folder as the
program that wrote the file. Open it using the notepad.

17
OUTPUT FILES

• Writing output to a text file : Example 2


Content of example.txt text file :
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
ofstream myfile ("example.txt");
if (myfile.is_open())
{ myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}
18
INPUT FILES

• Input files can be created using any text editor. Notepad


is an example of text editor you can use to save your
data.

• To create an input file, open the notepad and type the


data on it. Save the file in the same folder with the
program that will use these data. Example of the file is
shown below:

19
INPUT FILE Content of L4_3A.dat file:
• Reading data from a text file: Eg 1
# include <iostream>
# include <fstream>
#include <iomanip>
using namespace std;
int main()
{
double x; Content of L4_3B.dat file :
int i,j;
ifstream infile1("L4_3A.dat");
ifstream infile2("L4_3B.dat");

infile1>>i;
infile1>>j>>x;
infile1.close();
cout<<"From first file i = "<<i<<", j = "<<j<<", x = "<<x<<endl;

infile2>>i;
infile2>>j>>x;
infile2.close();
cout<<"From second file i = "<<i<<", j = "<<j<<", x = "<<x<<endl;
return 0;

Output displayed :
20
INPUT FILE
• Reading data from a text file: Eg 2 Content of example.txt text file :
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{ string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{ while (! myfile.eof() )
{ getline (myfile,line);
Output displayed :
cout << line << endl;
}
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}

21

You might also like