Published on December 29, 2010
File Handling in C++ : File Handling in C++ File Handling in C++ : File Handling in C++ Introduction to File Handling Data entered once and required later again. Same data to be used by others. Data required again by the same program. Files and Streams : Files and Streams Program Input Stream Output Stream Files I/O Streams : I/O Streams Stream Description cin Standard input stream cout Standard output stream cerr Standard error stream File I/O Streams : File I/O Streams Stream classes required for File I/O : ifstream ofstream fstream ifstream : ifstream Input file stream class Open() is a member function of the class ifstream Inherited functions of ifstream class from the class istream are: get() getline() read() seekg() tellg() Ofstream : Ofstream Output file stream class. Open() is a member function of the class ofstream. Inherited functions of ofstream class from the class ostream are put() write() seekp() tellp() fstream : fstream Supports files for simultaneous input and output. fstream is derived from ifstream ofstream iostream They are all parent class and fstream is the child class Fstream : Fstream Member functions for fstream are: open close closeall seekg seekp tellg tellp Opening a File : Opening a File A file can be opened by using any of the following two ways: Using the constructor function of the class Using the member function open() of the class OPENING A FILE USING CONSTRUCTOR: : OPENING A FILE USING CONSTRUCTOR: As we know a constructor is used to initialize an object while it is being created. Same as we can open a file by supplying the name of the file to the constructor of the file stream as follows: Example: ofstream obj(“abc.jpg”); ifstream file(“LETTER.TXT”); fstream fileio(“answers.txt”); OPENING A FILE USING OPEN() : OPENING A FILE USING OPEN() As we know the function open() is the member function in all the file streams ( ifstream,ofstream & fstream). We can open a file of a particular type of file stream with its default mode or by providing the other modes explicitly, as follows: Example: ifstream obj; obj.open(“STORY.TXT”); ofstream file; file.open(“product.dat”); fstream abc; abc.open(“xyz.dat”,ios::binary|ios::io|ios::out); CLOSING A FILE: : CLOSING A FILE: A file is closed using the member function close(), as follows: Example: ifstream obj; ----------- ----------- obj.close(); FILE MODES: : FILE MODES: The file mode parameter in open() and constructor function specifies the purpose for which the file is opened. If we don’t supply the mode parameter when we open a file it uses the default values in the absence of the actual value. The default values are as follows: ios::in for ifstream functions meaning open for reading only. ios::out for ofstream functions meaning open for writing only. OTHER FILE MODES & THEIR MEANING: : OTHER FILE MODES & THEIR MEANING: Parameter Meaning ios::app - Append to end of file. ios::ate - Go to end of file on opening ios:binary - Binary file ios::in - open file for reading only ios:nocreate - open fails if the file does not exist ios::noreplace - open fails if the file already exists ios::out - open file for writing only ios::trunc - delete the contents of the file if it exists SEQUENTIAL INPUT AND OUTPUT OPERATIONS: : SEQUENTIAL INPUT AND OUTPUT OPERATIONS: 1.The file stream classes support a number of member functions for performing the input and output operations on files. There are two pair of functions for this purpose-put() & get() functions : these functions are designed for handling a single character at a time. syntax: obj.put(ch); obj.get(ch); SEQUENTIAL INPUT AND OUTPUT OPERATIONS: : SEQUENTIAL INPUT AND OUTPUT OPERATIONS: 2. write() & read() functions: these functions are designed to write and read blocks of binary data. syntax: obj.write((char*)&var, sizeof(var));obj.read((char*)&var, sizeof(var)); Here var is an variable of either class or structure.