Pages

Saturday, August 20, 2011

File Handling

File is a collection of data or bytes.  The collection can be in the form of  characters, words, lines, paragraphs and pages,fields,records, image, audio or video. The meaning attached to a particular file is determined entirely by the data structures and operations used by a program to process the file. It is conceivable (and it sometimes happens) that a graphics file will be read and displayed by a program designed to process textual data. The result is that no meaningful output occurs (probably) and this is to be expected. A file is simply a machine decipherable storage media where programs and data are stored for machine usage. It is used to store any information permanently on a secondary storage device such as a hard disc, floppy, CD/DVD or flash drive. We need to store data on a device because the data entered by the user or processed by the program is stored in RAM. Since RAM is a temporary memory, it will store the data only as long as the computer or the program is running. If you switch off the computer or close the program, all the data entered by the user will be lost and will have to entered again. Entering the data again and again is not only time consuming but also impractical since it does not make any sense to enter the data each time the program is run.
File Handling is the process of storing data in a file so that it can be used later. Using a file handling program we can,
  1. Create a file.
  2. Store data in the file.
  3. Read the data.
  4. Edit the data.
The main benefit of storing data in a disc file is that it is "permanent" and will store data till it is deleted by the user.

Streams : Stream is usually flow of a water body from one place to another. With reference to C++ file handling, a stream is used to move data from one location to another.Streams act as an interface between files and programs.They represent as a sequence of bytes and deals with the flow of data.
Every stream is associated with a class having member functions and operations for a particular kind of data flow. All the file handling operations in C++ use different types of stream for transferring data between different stations. Normally, the data is required to be flown(moved) between
  1. Memory To File (Writing)
  2. File To Memory (Reading)
  3. Memory To File and File To Memory (Writing and Reading)
In C++, there are different streams for all the above purposes. When working with files in C++, the following classes can be used:
  1. Ofstream : Ofstream or Output Stream is used to "output" or send the data from the program to a disc file. In simple words, this class is used for "Writing" purpose. The term "Writing" refers to either creating a new file or editing the contents of an existing file.
  2. Ifstream : Ifstream or Input Stream performs the task of "Reading" the contents of a file. Using "Ifstream" a file can be read either fully or partially. It is to be remembered that if the has been opened for "reading" we can not perform any "writing" task on it.
  3. Fstream : The "Fstream" or File Stream can be used for both "Writing" and "Reading" purposes.  If the user wishes to perform both the tasks in a program then instread of using both "Ifstream" and "Ofstream", the use of "Fstream" is recomended since it saves time as the file is not required to closed and reopened in for performing different operations.

0 comments:

Post a Comment