Vous êtes sur la page 1sur 31

File Handling

By: Assad Abbas

The

output of C++ programs is held in main memory. The contents of output remain in memory as long as the program is running. If we are interested in permanently holding output even when the program is not being executed, then there needs to store the output in Files. We will be focusing on file streams in this lecture

15-2

There are three types of stream class objects that can be used for working with files. These are:

ifstream ofstream fstream

These classes are derived from istream, ostream and iostream classes respectively. Note that:
An ifstream object represents a file stream to read An ofstream object represents a file stream to write An fstream object represents a file stream to read or write

An output file is represented by an ofstream object. The general form to create an output file object is: ofstream objName (fileName); After the execution of this statement, the file fileName will automatically be opened for writing. So we can write to it immediately in the default mode (text mode). If the file fileName does not exit, it will be created. If it already exists, any data that was in the file is discarded and the data that we are going to write to file will form its new contents.

15-4

Even

if we do not write to the file after opening it, the original contents will be discarded and we will have an empty file.
objectobjName behaves just like cout except it does not write to output screen. It only writes to the specified file. that while specifying the path for the file, we need to use double back slashes \\ instead of single back slash \, because a single \ starts an escape sequence
15-5

The

Remember

If we do not specify a path, the file is assumed to be in the current directory. When an ostream object is destroyed, the file associated with this object is closed automatically. So our file will be closed when objName object goes out of scope. We can also close our file explicitly using the close() function. The general form to use this function is: objName.close(); Similarly, a file can be opened explicitly using the open() function. The general form to use this function is: objName.open(fileName);

15-6

Example

demonstrates how to write on disk

#include<iostream> #include<fstream> using namespace std; int main(){ ofstream out(c:\\myfolder\\test.txt); char name [10]; int experience; char city [10]; cout<<Enter Your Name(Without Space):; cin>>name; cout<<Enter Your City(Without Space):; cin>>city; cout<<Enter Your Experience(Without Space):; cin>>experience; out<<name<<\t<<city<<\t<<experience<<endl; return 0; }
15-7

// Opening a file to write data #include<iostream>


#include<fstream> using namespace std; int main(){ char* file = c:\\myfolder\\test.txt; ofstream out; out.open(file); char name [] = Abbas; char city [] = Lahore; int experience = 2; out<<name<<\t<<city<<\t<<experience<<endl; out.close(); return 0; }
15-8

To read from a file, we create an object of type ifstream and associate it with the file to be read. The general form to do so is: ifstream objName(fileName); The file we are going to read must already exist. The path for the file can be specified by the use of double back slashes \\. Reading a file in text mode is just like reading from cin. The object objName uses extraction operator to read data from the file. The eof( ) function is used to determine the end of the file. It returns true when end of file is reached.

15-9

//Demonstrating a read operation #include<iostream> #include<fstream> using namespace std; int main(){ char name [10]; char city [10]; int experience; ifstream in(c:\\myfolder\\test.txt); while (in>>name>>city>>experience){ cout<<name<<\t<<city<<\t<<experience<<endl; } return 0; }

15-10

10

Before we use a file, we need to check that it has been opened successfully. Some of the techniques to apply verification checks are:
Call the is_open( ) member of the ifstream object, which returns true if the file is open and false if it is not. Call the fail( ) function, which returns true if any file error occurs. Use ! operator with file stream object. When applied to the stream object, it returns true if the stream is not in satisfactory state.

The cerr Object The cerr object is usually used for printing error messages. This object is similar to the cout object. The difference is that unlike cout object, the output sent by cerr object is not buffered and is displayed immediately.
15-11

11

The file open mode for an ifstream or ofstream object determines what kind of operations can be performed on the file. It is determined by combination of bit mask values. These bit mask values are defined in ios_base class and inherited in the ios class as value of type openmode. These mask values are: ios::app Move to the end of the file before each write (append). ios::ate Move to the end of the file when opening it (at end). Used for reading and writing at the end of file

15-12

12

ios::binary Set binary mode rather than text mode. In binary mode, all characters are unchanged when they are transferred to or from the file. ios::in Open the file for reading ios::out Open the file for writing ios::trunc Truncate the existing file to zero length. These bit masks can be OR-ed. For a file to be opened for writing in binary mode such that data can only be added at the end of the file, the following mode value can be used: ios::out | ios::app | ios::binary

15-13

13

A file can also be opened both for reading and writing by specifying the following open mode values: ios::in | ios::out A file open mode can be set by specifying open mode value as a second argument to the file stream class constructor. i.e., ifstream(fileName, openmode); e.g., ofstream(test.txt, ios::out | ios::app); Both the ifstream and ofstream constructors have a second parameter of type openmode. The default value for openmode for an ifstream object is ios::in i.e., it opens the file just for input. Similarly, the default value for an ofstream object is

ios::out | ios::trunc i.e., it opens a file of length zero for output.


15-14

14

The

data is written in binary format with the write() function of ofstream (or fstream) object. Similarly, binary data is read using the read() function of ifstream (or fstream) object.
functions read and write data in bytes. All types of data like struct, array and class object etc. can be stored in to the file or read from the file.

These

15-15

15

The

general format of these functions is:

object.write((char*) &obj, sizeof(obj)); object.read((char*) &obj, sizeof(obj)); Here, object represents an object of ofstream or ifstream. char* represents the data address of fixed length in bytes of character type. obj represents a variable or object that contains data. It may be of any data type. sizeof(obj) returns the size of the variable or object.
15-16

16

//Program to write data in binary mode


#include<iostream> #include<fstream> using namespace std; class employee{ Public: char id[15], fName [10], lName[10]; int pay; }; int main(){

employee emp;
char choice; long i = 0; ofstream rec("employee.dat", ios::binary|ios::app); if(!rec){ cout<<"File opening error"<<endl; return 1; }
15-17

17

do{ ++i; cout<<"\nRecord#:"<<i<<endl<<endl; cout<<"Enter Your ID:";

cin>>emp.id;
cout<<"Enter Your First Name:"; cin>>emp.fName; cout<<"Enter Your Last Name:"; cin>>emp.lName; cout<<"Enter Your Pay:"; cin>>emp.pay; rec.write((char*)&emp, sizeof(emp)); cout<<"\nAdd More[Y/N]:"; cin>>choice; }while(choice == 'y' || choice == 'Y'); cout<<"\nTotal Records Entered:"<<i<<endl; rec.close(); return 0; }
15-18

18

//Program to read data from a file in binary mode #include<iostream> #include<fstream>

#include<string>
using namespace std; class employee{ public: char id[15],fName[10],lName[10]; int pay; }; int main(){ employee emp;

long i = 0;
ifstream in("employee.dat", ios::binary); if(!in){ cout<<"File opening error."<<endl; return 1; }
15-19

19

while(in.read((char*)&emp,sizeof(emp)))

{
if(strcmp(emp.id,"")!= 0){ ++i; cout<<"\nRecord #:"<<i<<endl<<endl; cout<<"ID: "<<emp.id<<"\n"<<"First Name: "<<emp.fName<<"\n" <<"Last Name: "<<emp.lName<<"\n"<<"Pay: "<<emp.pay<<endl; } // Skipping deleted records

}
in.close(); cout<<"\nTotal Records Read: "<<i<<endl; return 0; }

15-20

20

There are two things that we can do in relation to stream position. We can obtain and record the current position. Also we can change the current position. The current position is returned by the tellg() function for input stream objects and by tellp() function for output stream objects. Both functions return a value of type pos_type, which represents an absolute position in a stream. The general form to obtain the current position in an input file stream object in is: pos_type hereg = in.tellg(); //record the current file position. This position is recorded in a variable hereg. Similarly, the general from to obtain the current position in an output file stream object out is: pos_type herep = out.tellp();

15-21

21

We can define new position in a stream using either of the following methods: Passing the position recorded by tellg() to the seekg() member function for an input stream object i.e., in.seekg(hereg); //set current position to here Passing the position recorded by tellp() to the seekp() member function for an output stream object i.e., out.seekp(herep); The pos_type value is an integer value that corresponds to a character index position in the stream. The first character is always at index position 0. So it is possible to use numeric values to move to specific positions in the stream.

15-22

22

An

alternative approach to move to a new position in a stream is to specify an offset value relative to one of three specific positions in the stream. The offset can be +ve or ve. We can define a new position relative to the first character in the stream. This offset must be +ve. Similarly, relative to the last character in the stream, an offset can be defined. This offset must be ve.

15-23

23

15-24

24

To set a relative position, two arguments are passed through seekg() or seekp(). The first argument is the offset, which is an integer value of type off_type and the second argument must be one of the following values:
ios::beg. Here, offset is relative to the first character in the file. ios::cur. Here, offset is relative to the current position. ios::end. Here, offset is relative to the last character in the file.

We can specify the offset as an explicit integer constant or we can provide the value of the offset as an expression that evaluates to an integer. We can combine the seek operation with an input operation by using an extraction operation e.g., in.seekg(10, ios::beg)>>value; This statement moves the file position to an offset of 10 characters from the beginning of the file and reads from that point in to value.
15-25

25

#include<iostream> #include<fstream> #include<string> using namespace std; class employee{ char id[15],fName[10],lName[10]; int pay; }; int main(){ employee emp; char cc[15],choice;

long index = 1,counter;


ifstream in("employee.dat", ios::binary); if(!in){ cout<<"\nFile opening error"<<endl; return 1; }
15-26

26

do{ cout<<"\nEnter the ID:"; cin>>cc; in.clear(); in.seekg(0); counter = 0; while(in.read((char*)&emp,sizeof(emp))){ if(strcmp(cc, emp.id)== 0){ in.seekg((index)*sizeof(emp)); // set current position to here cout<<emp.id<<"\t"<<emp.fName<<"\t"<<emp.lName<<"\t" <<emp.pay<<endl; counter = 1; index++; if(counter == 0) cout<<"\nRecord not found"<<endl; cout<<"\n\t\t\tAny More(y/n)?:"; cin>>choice; }while(choice == 'y' || choice == 'Y'); cout<<"\nGood Bye"<<endl<<endl; return 0; }
15-27

// resetting in // setting position at the beginning of file

} }

27

#include<iostream> #include<fstream>

#include<string>
#include<ctype> using namespace std; struct employee{ char id[15],fName[10],lName[10]; int pay; }; int main(){ employee emp; char cc[15],choice; long index = 0,counter; fstream in("employee.dat", ios::binary|ios::in|ios::out); if(!in){ cout<<"\nFile opening error"<<endl; return 1; }
15-28

28

do{ cout<<"\nEnter the ID:"; cin>>cc; in.clear(); counter = 0; while(in.read((char*)&emp,sizeof(emp))){ index++; if(strcmp(cc, emp.id)== 0){ char sure = ' '; cout<<"Record to be deleted is:"<<endl; cout<<emp.id<<"\t"<<emp.fName<<"\t" <<emp.lName<<"\t"<<emp.pay<<endl; cout<<"Are You Sure(y/n)?"; cin>>sure; if(tolower(sure)== 'y'){ strcpy(emp.id , ""); strcpy(emp.fName , ""); strcpy(emp.lName , ""); emp.pay = 0; in.seekp((index-1)*sizeof(emp)); in.write((char*)&emp,sizeof(emp)); cout<<"Record Deleted Successfully"<<endl; counter = 1; } }
15-29

// resetting in in.seekg(0); // setting position at the beginning of file

29

if(counter

== 0)

cout<<"\nRecord not found"<<endl; cout<<"\n\t\t\tAny More(y/n):"; cin>>choice; }while(choice == 'y' || choice == 'Y'); cout<<"\nThanx! Allah Hafiz."<<endl<<endl; return 0; }

15-30

30

15-31

31

Vous aimerez peut-être aussi