A file can be opened in two ways :-
(a) Using the constructor of the stream class – This method is useful when only one file is used in the stream. Constructors of the stream classes ifstream, ofstream and fstream are used to initialize the file stream object with the file name.
For example, ifstream read_file(“Names.Dat”);
(b) Using the function open() - This method is useful when we want to use different files in the stream. If two or more files are to be processed simultaneously, separate streams must be declared for each.
For example, ifstream ifl; //input stream ifl created ifl.open(“Names.Dat”); // file Names.Dat linked with ifl
Second method is preferred over first method when there is a situation to open more than one file.