Define File Handling,File Handling & File Opening mode

What is file Handling? Explain in detail How to achieve File Handling.

File handling is a process of creating, opening, reading, writing or amending a stored file using some functions of any programming language. In C++, file handling is a process of manipulating files by using different operations. 

define file handling and define how to achieve file handling and mode of file opening

File Handling concept in C++ language is used for store a data permanently in a computer. Using file handling we can store our data in Secondary memory (Hard disk).

File Handling is used and important in Computer for permanent storage, and transfer of input - data or output - data from one computer to another can be easily done by using files. 

How to achieve File Handling? 

For achieving file handling in C++ we need to follow the following steps: 
  • Opening a file, 
  • Reading a file,
  • Writing to a file, and  
  • Closing a file.

i) Opening a file: 

Before performing any operation on a file the file must be opened. To open a file open() function is used.  Syntax:; 

ii) Reading a file:

To read a word from a file is called a reading operation.  The following syntax is used to read a word from the file. Syntax:  myFile>>c; where c is a character array.

iii) Writing to a file: 

ofstream handles file output (writing to files). Syntax: std::ofstream ofs ("filename", std::ofstream::out); 

iv) Closing a file: 

Once we have read a file. It must be closed. We can perform a closing operation on file by using the following statement:  myFile.close();   

What is meant by the term mode of File Opening? Describe different modes of an opening file. 

When we want to open a file we tell the compiler that for what purpose we want to open that file i.e. read, write or modify etc. These different methods/operations, used to open a file are referred to as a Mode of File Opening

In order to open a file in the desired mode, the member function open() should take mode as an argument along with the file name. 
Syntax: open (filename, mode); the following are different modes of opening a file.

ios::app mode give a description about append mode, all output to that file to be appended to the end.

ios::ate mode give a description about opening a file for output and move the read/write control to the end of a file.

ios::in mode give a description about opening a file for reading.

ios::out mode gives a description about opening a file for writing.

ios::trunk mode gives a description about if the file exists already, its content will be truncated before opening a file.

ios::binary mode gives a description about opening a file in binary mode.

What is the meaning of Stream in File Handling?

A stream is a sequence of data elements made available over time. A stream can be thought as a sequence of bytes of infinite length that is used as a buffer to hold data to be processed. In C++ stream is a sequence of bytes associated with a file. There are two types of streams: 

i) Input Stream: 

This stream is used to take any sequence of a file from an input device such as keyboard or file. The act of taking data from input devices is called extraction and the operator is used for this purpose is called stream extraction operator >>.  The standard input stream in C++ is cin.

ii) Output Stream: 

The output stream is used to display on an output device or file. The act of displaying data on the screen is called insertion and the operator used for this purpose is called Stream insertion operator <<. The standard output stream object in C++ is cout. cerr and clog are also used as stream objects.

Post a Comment