The methods of opening file within C++ program:
- Opening a file using constructor
- Opening a file using member function open() of the class.
Opening a file using constructor:
The syntax for opening file for output purpose only is
ofstream obj(“filename”);
where ofstream is a class and obj is a object of type ofstream and “filename” is any valid name of a file to be opened for output purpose only.
Example:
ofstream fout(“results.dat”);
Opening file using open():
The syntax for opening file for output purpose only
ofstream object.open(“filename”);
where ofstream is a class and object is of ofstream class type and filename is any valid file to be opened for output purpose only.
Example:
ofstream obj;
obj.open(“text.dat”);