220 likes | 531 Views
Data File Handling. INTRODUCTION . A file is a collection of related data stored in a particular area on the disk. Programs can be designed to perform the read and write operations on these files. A program typically involves either or both of the following kinds of data communication:.
E N D
INTRODUCTION A file is a collection of related data stored in a particular area on the disk. Programs can be designed to perform the read and write operations on these files.
A program typically involves either or both of the following kinds of data communication: Data transfer between the console unit and the program. Data transfer between the program and a disk file.
Stream • A Stream is sequence of bytes or in other word stream is a flow of bytes into or out of a program. Generally three streams are used for file I/O. These are: • Ifstream : It is derived from istream class and is used to associate a file with an input buffer so that the file can be read from. • ofstream:It is derived from ostream class and it is used to associate a file with an output buffer so that the file can be written onto. • fstream : it is derived from iostream class and it is used to assocaite a file with a buffer so that the file can be read from as well as written onto.
Opening and Closing a File If we want to use a disk file, we need to decide the following things about the file and its intended use: • Suitable name for the file • Open the file • Process the file • Close the file
A file can be opened in two ways: 1. Using the constructor function of the class 2. Using the member function open() of the class The first method is useful when we use only one file in the stream. The second method is used when we want to manage multiple files using one stream.
OPENING A FILE 1. By using the CONSTRUCTOR of the stream class. ifstream transaction(“sales”); ofstream result(“result”); 2. By using the open() function of the stream class ifstream transaction; transaction.open(“sales”);
Closing of File Stream_name.close(); e.g., transaction.close();
Types of Files • . The two basic types are • text and • binary. • A text file stores information in ASCII characters. In text files each line of text is terminated with a special character known as EOL. • A binary file is just a file that contains in the same format in which information is held in memory. In binary file there is no delimeter.
File Mode Parameters Ifstream fin; fin.open(“abc.txt”,ios:in) fin.open(“abc.txt”,ios:app | ios:nocreate);
File Pointers and Their Manipulations The C++ input and output system manages two integer values associates with a file. These are: • get pointer – specifies the location in a file where the next read operation will occur. • put pointer – specifies the location in a file where the next write operation will occur. In other words, these pointers indicate the current positions for read and write operations, respectively. Each time an input or an output operation takes place, the pointers are automatically advances sequentially.
Binary file operations • Writing/Reading Data in Binary Format • To write and read data in binary format two member functions are available in • C++. They are read( ) and write( ). • Fileobject.write( (char *)&object, sizeof(object)); • Fileobject.read( (char *)&object, sizeof(object)); Example of write ( ) member function
To Read data from a binary File using read( ) member function • #include<fstream> • #include<iostream> • #include<conio.h> • using namespace std; • struct student • { int roll ; • char name[30]; • char address[60]; • }; • void main() • { student s; • ifstream fin; • fin.open("student.dat"); • fin.read((char *)&s,sizeof(student)); • cout<<"\n Roll Number :"<<s.roll; cout<<"\n Name :"<<s.name; cout<<"\n Address :"<<s.address; fin.close(); • getch(); • }
Writing Class object in a file • class student • { int roll ; • char name[30]; • char address[60]; • public: • void read_data( ); }; • void student::read_data( ) // member function defintion • { cout<<"\n Enter Roll :"; • cin>>roll; • cout<<"\n Student name :"; • cin>>name; • cout<<"\n Enter Address :"; • cin>>address;} • void student:: write_data() { cout<<"\n Roll :"<<roll; • cout<<"\n Name :"<<name; • cout<<"\n Address :"<<address;} • void main() • { • student s; ofstream fout; fout.open("student.dat"); • s.read_data(); // member function call to get data from KBD • fout.write((char *)&s,sizeof(student)); // write object in file • fout.close(); • }