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
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot
8 min read
C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and
9 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
C++ Polymorphism The word polymorphism means having many forms. A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So, the same person exhibits different behaviour in different situations. This is ca
5 min read