Beruflich Dokumente
Kultur Dokumente
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
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
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)