Open In App

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

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:

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:



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:

Below is the C++ program to implement the close() function:




// 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:


Article Tags :