Open In App

std::fstream::close() in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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:



Last Updated : 02 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads