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
ostream
istream 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:
• ios::out
it is specifies the name of the file, that is to be opened followed by file extension 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// filewrite.cpp // An example writing data into file #include #include #include #include <stdlib.h> int main() { ofstream outf; int i;
//used for exit()
outf.open(“file.txt”); if (!outf) { cout<<“ERROR”<<endl; exit(1); } cout<<“Start of program\n\n"; for (i=0; i<10; i++) outf<<<“; outf.close();
//terminate with error
cout<<“\nEnd of program\n”; return 0; // indicates successful termination } // 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// filewrite.cpp // An example writing data into file #include #include #include #include <stdlib.h> //used for exit() int main() { ofstream data ("records.txt", ios::out); //create file and attach to object “data” char name[15]; int age, i; i=1; while (i<=5) { clrscr(); cout<<"Enter Record# "<<<endl; cout<<"Enter name: "; cin>>name; cout<<"Enter age: "; cin>>age; data<
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:
• ios::in
it is specifies the name of the file, that is to be opened followed by file extension 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// filewrite.cpp // An example reading data from formatted I/O file #include #include #include #include <stdlib.h> //used for exit() int main() { int sum=0; int x=10; ifstream inf; inf.open(“file.txt”); if (!inf) { cout<<“Cannot open file”; exit(1); //terminate with error } cout<<“Start of program\n\n"; while (inf >> x) sum= sum + x; inf.close(); cout<<“Sum = “ <<sum<<endl; cout<<“\nEnd of program\n”; return 0; // indicates successful termination } // 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)