Search This Blog

FILE HANDLING

FILE HANDLING

File handling is required to store the processed data in a file which may be accessed later on when required.

Files are of two types :

Text file: A text file stores information in readable and printable form. Each line of text is terminated with an EOL (End of Line) character.
Binary file: A binary file contains information in the non-readable form i.e. in the same format in which it is held in memory.

We need to learn few terms for handling files. Data in accessed in files as a stream of bits.

Stream: A stream is a general term used to name flow of data. Different streams are used to represent different kinds of data flow.

Classes used for file handling

Three main classes are used for file I/O i.e.  read / write operations:

  • ifstream - This class is used for read operatios. Objects of this class read data from the file.
  • ofstream - This class is used for read operatios. Objects of this class write data on a file.
  • fstream - Objects of this class are capable of reading as well as writing onto the file.

Header file used for file handling

 fstream.h
-          This header includes the definitions for the stream classes ifstream, ofstream and fstream.
-          It contain predefines set of operations for handling input and output in files. fstream.h class ties a file to the program for input and output operation.



Opening a File in C++:

a.  By the Constructor Method. This method is preferred when single file is used with the stream. (only for input / only for output)
b.  By the open() function of the stream.



File modes:
• ios::out - It creates file in output mode and allows writing into the file.
• ios::in - It creates file in input mode and permit reading from the file.
• ios::app - To retain the previous contents of the file and to append to the end of existing file.
• ios::ate - To place the file pointer at the end of the file, but you can write data any where in the file.
• ios::trunc - It truncates the existing file (empties the file).
• ios::nocreate - If file does not exist this file mode ensures that no file is created and open() fails.
• ios::noreplace - If file does not exist, a new file gets created but if the file already exists, the open() fails.
• ios::binary – Opens a file in binary.

Common functions associated with a file

Detecting End of file 
eof(): This function determines the end-of-file by returning true for end of file otherwise returning false.
Opening a file using open () method
open():If you want to manage multiple file with same stream use open().

File object_object.open(“Filename”,(Filemode)); 
  e.g., fstream fobj;
          fobj.open(“book.dat”, ios::out | ios::in | ios::binary);


Closing a file using close() method
close(): This function terminates the connection between the file and stream associated with it.
Syntax :
            File_object.close();
eg:      fstream fobj(“book.dat”, ios::in | ios::binary);
           fobj.close()          ;

Reading a block of data from a file
read(): The read() function reads a fixed number of bytes from the specified stream and puts them in the buffer.
Stream_object.read((char *)& Object, sizeof(Object));

Writing a block of data from a file
write(): The write() function writes fixed number of bytes from a specific memory location to the specified stream.
Stream_object.write((char *)& Object, sizeof(Object));
(char *)& Object    :  The first is the address of variable. The address of variable must be type cast to type char*(pointer to character type)

To calculate size of the block to read/ write
sizeof(Object) :   The second is the length of that variable in bytes.
The data written to a file using write( ) can be accessed in C++ prgoram using read( ) function.



Pointers associated with a file
get pointer: A get pointer indicates the position in the file at which the next input is to occur.
put pointer: It indicates the position in the file at which the next output is to be placed.
seekg(): It moves get pointer to the specified position in a stream.
seekp(): It moves the put pointer to the specified position in a stream.
tellg(): This function returns the current position of the get pointer in a stream.
tellp(): This function returns the current position of the put pointer in a stream.

  File Handling Steps:

· Declare a file stream object as per the operation to be performed on the file (read or write).
· Open the file using appropriate mode.
· Process the file. 
· Close the file.

No comments:

Post a Comment