Sie sind auf Seite 1von 26

Week 14-15

Outline
• File Handling
• Storing and reading data from the data files

1
File Handling
• Stream is a general term
File Handling
• C++ language is capable of providing a
huge set of I/O stream
• The ios class is the foundation of all I/O
operations
• But there are four basic I/O classes which
are pictured in the fig ahead
• It gives us the facilities for checking the
state I/O streams and especially the
output formatting facilities
File Handling(cont)
• The ios class contains some important
features, some we have already seen in
use, but the topic of discussion is one of
the three most imperative features

– File operation modes


– Formatting flags
– Error-status flags
Stream Input/Output Classes
• Four basic I/O classes

basic_ios

basic_istream basic_ostream

basic_iostream

5
fstream.h file
• When a file is opened, a stream object is associated
with the filename
• fstream.h: The header file fstream.h contains the
definitions of these objects. There are basically three
kinds:
• ifstream class: the objects of this class can be used to
read data from a named file on disk into the computer
memory
• ofstream class: the objects of this class can be used to
write data from the computer memory into a named file
on the disk
• fstream class: this stream objects are used for both
input & output operations on files on the disk
ios class
• The extraction operator “>>” that is used
for input operations is a member of the
istream class
• The extraction operator “<<” that is used
for output operations is a member of the
ostream class
• Both istream and ostream classes are
derived form the ios class
ios stream class

ios

istream ostream

iostream

ifstream fstream ofstream


File operation modes
• The data files can be opened into different
modes
• These modes are defined in “ios” class
• The resolution operator “::” is used
between the ios mode and the mode code
File operation modes and their
uses
• ios::out : opens file in output mode to write data
into the file. If the file with same name exists on
the disk (in the same directory), all data in that file
is deleted and the file is opened as a new file
• ios::app: opens file in output mode to append
(add) data at the end of the file. Does not delete a
same name file (within the same directory). If file
doesnot exist then a new file with the specified
name is created
• ios::ate: move the file pointer to the end of the
file, when the file is opened
File operation modes and their
uses (cont)
• ios::in: opens file in input mode to read data from the
file
• ios::trunc: the data of the file is deleted if file already
exists on the disk. It is also the default setting for
“out” mode
• ios::nocreate: no new file is created. If file does not
exist, the ‘file open’ operation fails
• ios::noreplace: the file is not replaced, if already
exists with same name on the disk. If the file with the
same name exists, the open operation fails
• ios::binary: opens the file in binary mode. The data
is accessed and stored in binary format
Formatted I/O
• In formatted I/O, data is stored in the file as a
series of characters
• One character or digit takes one byte to store on
the disk, thus the number takes 2184.456 takes
8 bytes – one byte for each digit including
decimal point
• Formatted I/O are also known as “sequential
access files”
• In sequential access files, data is read in a
sequence. i.e. beginning of file till the end
Formatted I/O (cont)
• “<<“ insertion operator is used with “cout”
object to send output to the screen or to
send the output to the data file on the disk
• “>>“ operator is also used with ifstream (or
fstream) class to read the data from the
file
• When the program ends, the object
created for the file is also destroyed and
the data file attached with the object is
automatically closed
Opening files in output mode
(writing mode)
• To open a data file “filename” in output mode, an object of the
ofstream (or fstream) class is created
• The syntax to open a data file for output in output mode is:
ofstream outf(“filename”, ios::out);
or
ofstream outf;
outf.open(“filename”);
• Where:
• outf is the name of the object of the
ofstream class that is to be created for
controlling of data streams of the file.
(i.e. stream buffer of that file“filename”)
Opening files in output mode
(writing) (cont)
• filename: it is specifies the name of the file,
that is to be opened followed by file
extension
• ios::out it specifies the mode of operation of
the file. The use of “ios::out” is optional
if the object of ofstream class is
created
An example of opening a file for writing
• 1 // filewrite.cpp
• 2 // An example writing data into file
• 3 #include <iostream.h>
• 4 #include <fstream.h>
• 5 #include <conio.h>
• 6 #include <stdlib.h> //used for exit()
• 7 int main()
• 8 {
• 9 ofstream outf;
• 10 int i;
• 11
• 12 outf.open(“file.txt”);
• 13
• 14 if (!outf)
• 15 {
• 16 cout<<“ERROR”<<endl;
• 17 exit(1); //terminate with error
• 18 }
• 19 cout<<“Start of program\n\n";
• 20 for (i=0; i<10; i++)
• 21 outf<<i<<“;
• 22 outf.close();
• 23
• 24 cout<<“\nEnd of program\n”;
• 25 return 0; // indicates successful termination
• 26 } // end main
Start of program

End of program
Open file for writing (cont)
• In the previous example, if the “file.txt” file
does exist it is created, but if it does exist
it is destroyed and new file is created in its
place
• “file.txt” file will now contain the following
data
0123456789
When you open the file for reading
An example of opening a file for writing
• 1 // filewrite.cpp
• 2 // An example writing data into file
• 3 #include <iostream.h>
• 4 #include <fstream.h>
• 5 #include <conio.h>
• 6 #include <stdlib.h> //used for exit()
• 7 int main()
• 8 {
• 9 ofstream data ("records.txt", ios::out); //create file and attach to object “data”
• 10 char name[15];
• 11 int age, i;
• 12 i=1;
• 13 while (i<=5)
• 14 {
• 15 clrscr();
• 16 cout<<"Enter Record# "<<i<<endl;
• 17 cout<<"Enter name: ";
• 18 cin>>name;
• 19 cout<<"Enter age: ";
• 20 cin>>age;
• 21 data<<name<<"\t"<<age<<endl; //send name and age input values to the data file
• 22 i++;
• 23 }
• 24 return 0; // indicates successful termination
• 25 } // end main
Enter Record# 1
Enter name: shehzad
Enter age: 28
Opening files in input mode
(reading)
• To open a data file “filename” in input mode, an object
of the ifstream (or fstream) class is created
• The syntax to open a data file in input mode is:
ifstream inf(“filename”, ios::in);
or
ifstream inf;
inf.open(“filename”);
• Where:
• inf is the name of the object of the
ofstream class that is to be created for
controlling of data streams of the file.
(i.e. stream buffer of that file“filename”)
Opening files in input mode
(reading) (cont)
• filename: it is specifies the name of the file,
that is to be opened followed by file
extension
• ios::in it specifies the mode of operation of
the file. The use of “ios::in” is optional
if the object of ofstream class is
created
An example of opening a file for
reading
• 1 // filewrite.cpp
• 2 // An example reading data from formatted I/O file
• 3 #include <iostream.h>
• 4 #include <fstream.h>
• 5 #include <conio.h>
• 6 #include <stdlib.h> //used for exit()
• 7 int main()
• 8 {
• 9 int sum=0;
• 10 int x=10;
• 11 ifstream inf;
• 12 inf.open(“file.txt”);
• 13
• 14 if (!inf)
• 15 {
• 16 cout<<“Cannot open file”;
• 17 exit(1); //terminate with error
• 18 }
• 19 cout<<“Start of program\n\n";
• 20 while (inf >> x)
• 21 sum= sum + x;
• 22 inf.close();
• 23 cout<<“Sum = “ <<sum<<endl;
• 24 cout<<“\nEnd of program\n”;
• 25 return 0; // indicates successful termination
• 26 } // end main
Open file for reading (cont)
• In this example
• We assume the following data in “file.txt”
file
• 10 20 30 40 50 60 70
Start of program

Sum = 280

End of program
Open file for reading (cont)
• In the previous example, the “file.txt” is
successfully opened (using open ()
method) and is now prepared for reading
• The program can read from the file
executing statement such as
while (inf >> x)

Das könnte Ihnen auch gefallen