std::fstream::close() in C++
Last Updated :
02 Nov, 2023
Files play an important role in programming. It allows storage of data permanently. The C++ language provides a mechanism to store the output of a program in a file and browse from a file on the disk. This mechanism is termed file handling. In order to perform file handling, some general functions which are used are as follows:
- open(): This function helps to create a file and open the file in different modes, like input operations, output operations, binary mode, etc.
- close(): This function helps to close an existing file.
- get(): This function helps to read a single character from the file.
- put(): This function helps to write a single character in the file.
- read(): This function helps to read data from a file.
- write(): This function helps us to write data into a file.
A stream is an abstraction that represents a tool on which operations of input and output are performed. A stream is often represented as a source or destination of characters of indefinite length, counting on its usage. So far, the header file which provides functions cin and cout is used to require input from the console and write output to a console respectively. In C++ there is a group of file handling methods. These include ifstream, ofstream, and fstream. These classes are obtained from fstreambase and from the corresponding iostream class. These classes are designed such that they are able to manage the disk files, declared in fstream, and thus this file must be included in any program that uses files.
fstream Library: Fstream is a library that consists of both, ofstream and ifstream which means it can create files, write information to files, and read information from files. This header file is generally used as a data type that represents the file stream. Which is used while describing the syntax to open, read, take input and close the file, etc.
How To Close File? In order to use a disk file for storing data, the following parameters need to be decided about the file and its intended use. The parameters that are to be noted are as follows:
- A name for the file.
- Data type and structure of the file.
- Purpose (reading, writing data).
- Opening method.
- Closing the file after use.
This article focuses on closing the file. In a case, if a C++ program terminates, then it automatically flushes out all the streams, releases all the allocated memory, and closes all the opened files. Therefore, it is a good alternative to use the close() function to close the file-related streams, and it is a member of ifsream, ofstream, and fstream objects.
Syntax:
close()
Properties:
- Return value: The close() function provides no return value which means that if the operation fails, including if no file was open before the call, the failbit state flag is set for the stream (which may throw ios_base::failure if that state flag was registered using member exceptions.
- Exception handling: When the function is provided with an exception and the stream is in a valid state, then any exception thrown by an internal operation is caught by the function and rethrown after closing the file. It throws an exception to member type failure only if the function fails (setting the failbit state flag) and member exceptions are set to throw for that state.
- It modifies the fstream object. Concurrent access to an equivalent stream may introduce data races.
Below is the C++ program to implement the close() function:
C++
// C++ program to implement close() function
#include <fstream>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
char data[100];
// Open a file in write
// mode.
ofstream outfile;
outfile.open("gfg.data");
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
// This function will take the entire
// the user enters and will store in
// the "data" array declare above
cin.getline(data, 100);
// Write inputted data into
// the file.
outfile << data << endl;
// Here we make use of the close()
// function to close the opened file
outfile.close();
// Open a file in read mode
ifstream infile;
infile.open("gfg.data");
cout << "Reading from the file"
<< endl;
infile >> data;
// Write the data at the screen
cout << data << endl;
// Close the opened file
infile.close();
return 0;
}
Output:
Similar Reads
std::ifstream::is_open() in C++ The std::ifstream::is_open() function in C++ is a member function of the std::ifstream class, which is used to check if a file stream object has successfully opened a file for reading or not. This function returns a boolean value, true if the file stream is open and ready for I/O operations, and fal
2 min read
How to Read a File Using ifstream in C++? In C++, we can read the contents of the file by using the ifstream object to that file. ifstream stands for input file stream which is a class that provides the facility to create an input from to some particular file. In this article, we will learn how to read a file line by line through the ifstre
2 min read
How to Open and Close a File in C++? 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 fst
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 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
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 Get Error Message When ifstream Open Fails in C++? In C++, the std::ifstream class is used to open a file for input operations. It associates an input stream to the file. However, due to some reasons, it may be unable to open the requested file. In this article, we will learn how to show an error message when the ifstream class fails to open the fil
2 min read
How to Remove Last Character From C++ String? In C++, strings are stored as the std::string class objects. In this article, we will look at how to remove the last character from this C++ string object. For Example, Input: Hello! GeeksOutput: Hello! GeekRemove the Last Character from a String in C++To remove the last character from a string, we
2 min read
cin.clear() function in C++ The cin.clear() function in C++ is a member function of the std::istream class, which is used to clear the error flags on the cin stream. When an input operation fails (for example, due to incorrect data type input), the cin stream enters a fail state, and further input operations are blocked until
4 min read
cin.ignore() Function in C++ The cin.ignore() function in C++ is a member function of the std::istream. It is used to ignore (or discard) certain number of characters from the input buffer. This function is very useful when we have to deal with leftover characters in the input stream that could interfere with subsequent input o
3 min read