C++ Close file
C++ File Handling close() Function
A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close the file currently associated with the object. The close() uses ofstream library to close the file.
Syntax
file-stream-class file-stream-object;
file-stream-object.close();
Example
ofstream file1;
file1 .close("employee.txt");
ifstream file1;
file1 .close("employee.txt");
C++ File Handling close() Function Example
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("employee.txt");
if(myfile.is_open()){
cout<<"File is open"<<endl;
myfile.close();//file close
cout<<"File close successfully."<<endl;
}
else{
cout<<"Error in file opening"<<endl;
}
return 0;
}
Output: