File Handling A File is collection of related

  • Slides: 9
Download presentation
File Handling • A File is collection of related records and record is information

File Handling • A File is collection of related records and record is information about an entity. Files are stored on secondary storage media (Hard Disk). • In normal programing data and information are stored in RAM and they are destroyed when exit the program. • To store the information and data permanently we use Disk I/O system. • C++ provide two types of file system: – Text Files – Binary Files. 1

Text File: • A text file is a file in which information are stored

Text File: • A text file is a file in which information are stored in ordinary text and human readable format. • The information stored in text file can be read with any ordinary editor. • The extension of these files is usually given as filename. txt Binary File: • In binary files information are stored in binary code ie the content of binary file can not be viewed by an ordinary editor. • Files with extension. dat, . doc, . xls etc are binary files. 2

File Access Methods There are two types of file accessing methods: 1. Sequential Access

File Access Methods There are two types of file accessing methods: 1. Sequential Access Method § In sequential access information are stored or accessed in a sequence ie one after other. § The length of each record is not fixed. It means each record store in different memory length. § The advantage of sequential access method is that memory is not wasted. Where as disadvantage is that searching process is slow. § Word files, audio tapes are example of sequential file. 3

2. Random Access Method: • This methods does not read or write information in

2. Random Access Method: • This methods does not read or write information in a sequence but in a fixed length record. • The advantage of random access methods is that search process is very fast. • Disadvantage is that there is a wastage of memory because memory is allocated according to record size even if a single character is entered in that record. • Database system, audio CDs store files in random access mode. 4

File Handling Streams File handling is performed with streams. Where as a stream is

File Handling Streams File handling is performed with streams. Where as a stream is an abstraction that represent a device on which input/output operation can be performed. Basically it represent source or destination of data. There are two types of stream in C++: 1. Input Stream. – This stream is used to input the data. This stream establish connection between the input device and the program. – The act of reading data from an input stream is called extraction. It is performed by extraction operator. Example cin>> 2. Output Stream – This stream is used to output the data. This stream establish connection between the output device and the program. – The act of writing data to an output stream is called insertion. It is performed by insertion operator. Example cout<<. 5

Hierarchy of Stream Classes IOS istream ostream ifstream ofstream 6

Hierarchy of Stream Classes IOS istream ostream ifstream ofstream 6

ios is main class, all others are derived from ios class (iostream. h). istream

ios is main class, all others are derived from ios class (iostream. h). istream class handles all the inputs and ostream class handles all the outputs. Ex: cin or cout fstream. h consist of definitions for the following classes: – ifstream (open file to read data from a file) – ofstream (open file to write data to a file) – fstream (open file to R/W data to file) 7

Opening a File • A file should be open first before performing any process

Opening a File • A file should be open first before performing any process (read, write) on it. A file can be opened by an object of ofstream, ifstream or fstream. • This object is then associated with a file. • Any operation (R/W) on the object is applied to the physical file associated with it. 8

Ofstream class ofstream class is used to write information in the file. Syntax: ofstream

Ofstream class ofstream class is used to write information in the file. Syntax: ofstream open_file(“file_name”, mode); or ofstream open_file; open_file. open(“test. txt, mode) #Include <fstream. h> Main() { ofstream number(“test. txt”); for(int i=1; i<+15; i++) number<<i<<endl; } In this example an object number of class ofstream is created. This object is associated with a file test. txt. We can write information in the file with object number. 9