C++ Open and closing file(I/O)
If we want to use a disk file, we necessary to decide the following things about the file and its intended use:
1. suitable name for the file
2. Data type and structure
3. purpose
4. opening method
The filename is a string of character of group of characters that make up a valid filename for the operating system
it may contain two parts, a primary name and optional period with extension Example
Input.data
Test.doc
INVENT.ORY
student
salary
OUTPUT
As started earlier for opening a file, we must first create a file stream and then link it to the filename. A file stream can be defined using the classes ofstream, fstream and ifstream that are contained in the header file fstream. The class to be used depends on the purpose, that is whether we want to read data from the file or write data into the file.
A file can be opened in two ways
1. using the constructor function of a class
2. using the function open() of the class
The first method is useful only when we use only one file in the stream. The second method is useful when we want to manage multiple files using one stream