Upload
paancute8982
View
256
Download
0
Embed Size (px)
Citation preview
8/6/2019 C++ File Input Output
1/21
1
BITG 1113:
Files : Input Output
LECTURE 10
8/6/2019 C++ File Input Output
2/21
2
To understand the fundamentals of files
and streams input/output
Able to write programs that use data files
for input and output.
Objectives:
8/6/2019 C++ File Input Output
3/21
3
FILES
File an external collection of related data treated as a
unit.
Purpose to keep a record of data.
record of data
8/6/2019 C++ File Input Output
4/21
4
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 modifiedbyother programs when necessary.
8/6/2019 C++ File Input Output
5/21
5
FILES
Two major classes of files:
Text files
Binary files
Files are stored in secondary storage devices. Two of itsmost common forms are:
Disk (Hard disk, CD, and DVD)
Tape
8/6/2019 C++ File Input Output
6/21
6
Files in a secondary storage can be both read andwritten. These processes are also known as I/O orinput/output.
Files exist separately from the computer and ourprograms, so we must be able to create a linkagebetween the external file and its usage in our program.
A stream is this linkage. Stream is an abstractrepresentation of input data source, or output datadestination, used in our program.
FILES & STREAM
8/6/2019 C++ File Input Output
7/21
7
FILES & STREAMS
To use files, input streams and/or output streams have tobe created depending on the required file operations.
Stream classes have been included in a
header file. This header file also automatically includesthe header file, so we no longer need toinclude it.
The type of stream class determines if it is an input file or
an output file orboth.
8/6/2019 C++ File Input Output
8/21
8
Input file are of type ifstream(Stream class to read from
files).
Output files are of type ofstream(Stream class to writeon files).
Input & output files are of type fstream(Stream class to
both read and write from/to files).
FILES & STREAMS
8/6/2019 C++ File Input Output
9/21
9
We can use our file streams the same way we use cin andcout, with the only difference that we have to associatethese streams with physical files.
// basic file operations#include
#include using namespace std;
int main ()
{ ofstream myfile("example.txt");//open file example.txt andassociate output stream object myfile with it.
myfile
8/6/2019 C++ File Input Output
10/21
10
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 computers monitor is:Writing this to the monitor.
FILES & STREAMS
8/6/2019 C++ File Input Output
11/21
11
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.
8/6/2019 C++ File Input Output
12/21
12
There are a few modes to open a file. They are listed asfollows: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 outputoperations are performed at the end of the file,
appending the content to the current content of thefile.
ios::trunc - If the file opened for output operations already existedbefore, its previous content is deleted and replacedby the new one.
INPUT/OUTPUT FILES : Openinga file
8/6/2019 C++ File Input Output
13/21
13
All these flags can be combined using the bitwise operator
OR (|).
For example, if we want to open the file example.bin toadd data we could do it by the following :
ofstream myfile("example.bin", ios::out | ios::app);
INPUT/OUTPUT FILES : Openinga file
8/6/2019 C++ File Input Output
14/21
14
For all stream classes, default mode/s is/areautomatically assumed, even if a mode that does notinclude them is passed as second argument to open thefile.
Streamclass Defaultmodeparameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
The default value is only applied if the file is openedwithout specifying any value for the mode parameter.
INPUT/OUTPUT FILES : Openinga file
8/6/2019 C++ File Input Output
15/21
15
When we are finished with our input and outputoperations on a file we shall close it so that its resourcesbecome available again.
In order to do that we have to call the stream's functionclose(). This function takes no parameters, and what itdoes 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 anotherfile.
INPUT/OUTPUT FILES : Closinga file
8/6/2019 C++ File Input Output
16/21
16
OUTPUT FILES
Writing output to a text file : Example 1
# include
# include
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
8/6/2019 C++ File Input Output
17/21
17
OUTPUT FILES
Content of L4_2.out text file :
You can find the L4_2.out file in the same folder as theprogram that wrote the file. Open it using the notepad.
8/6/2019 C++ File Input Output
18/21
18
Writing output to a text file : Example 2
#include
#include
using namespace std;
int main (){
ofstream myfile ("example.txt");
if (myfile.is_open())
{ myfile
8/6/2019 C++ File Input Output
19/21
19
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:
INPUT FILES
8/6/2019 C++ File Input Output
20/21
20
Reading data from a text file: Eg 1# include # include #include using namespace std;
int main(){
double x;int i,j;
ifstream infile1("L4_3A.dat");ifstream infile2("L4_3B.dat");
infile1>>i;infile1>>j>>x;infile1.close();
cout
8/6/2019 C++ File Input Output
21/21
21
Reading data from a text file: Eg 2#include #include
#include
using namespace std;
int main ()
{ string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{ while (! myfile.eof() )
{ getline (myfile,line);
cout