How to Open and Close a File in C++? Last Updated : 08 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, we can open a file to perform read and write operations and close it when we are done. This is done with the help of fstream objects that create a stream to the file for input and output. In this article, we will learn how to open and close a file in C++. Open and Close a File in C++ The fstream class contains the std::fstream::open() function that can be used to open files in different modes. Syntax of fstream::open()fstream_object.open(filename, mode); The modes can be of three types: std::ios::in: This mode is used to open a file for reading only.std::ios::out: This mode is used to open a file for writing. If there is already content in the file then it will be overwritten in this mode.std::ios::app: This mode is called the append mode in which we open a file for writing at the end of the file. We can close the file using std::fstream::close() function. Algorithm 1. We will first create an fstream object.2. Then we open the file using open() function in write mode(ios::out)2. We then write some data to the file.3. At the end, we close the file using close() function C++ Program to Open and Close a File C++ // C++ program to demonstrate how to open and close a file #include <fstream> #include <iostream> using namespace std; int main() { // create and ofstream object and open the file in // append mode ofstream fio("abc.txt", ios::app); // Check if the file is opened successfully if (fio.is_open()) { cout << "File opened successfully." << endl; // Append content to the file fio << "This text is appended to the file." << endl; // Close the file fio.close(); cout << "File closed." << endl; } else { // Display error if file was not opened cout << "Error opening file!" << endl; } return 0; } OutputFile opened successfully. File closed. Comment More infoAdvertise with us Next Article How to Open and Close a File in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ cpp-file-handling CPP Examples Practice Tags : CPP Similar Reads How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 2 min read How to Redirect cin and cout to Files in C++? In C++, we often ask for user input and read that input using the cin command and for displaying output on the screen we use the cout command. But we can also redirect the input and output of the cin and cout to the file stream of our choice. In this article, we will look at how to redirect the cin 2 min read How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read How to Read a File Line by Line in C++? In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by 2 min read How to Read File into String in C++? In C++, file handling allows us to read and write data to an external file from our program. In this article, we will learn how to read a file into a string in C++. Reading Whole File into a C++ StringTo read an entire file line by line and save it into std::string, we can use std::ifstream class to 2 min read Like