1 / 19

Data File Handling

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:.

lang
Télécharger la présentation

Data File Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data File Handling

  2. 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.

  3. 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.

  4. Program File Interaction

  5. 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.

  6. Classes for File stream operations

  7. 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

  8. 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.

  9. 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”);

  10. Closing of File Stream_name.close(); e.g., transaction.close();

  11. 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.

  12. File Mode Parameters Ifstream fin; fin.open(“abc.txt”,ios:in) fin.open(“abc.txt”,ios:app | ios:nocreate);

  13. 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.

  14. Functions associated with file pointers :

  15. 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

  16. 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(); • }

  17. 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(); • }

  18. Thank You

More Related