Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does move assign an std::fstream close the original stream

Since c++11, we can move assign one std::fstream object to another, but I'm unable to find documentation that states, what happens if the fstream object is already associated with a file (is_open()==true).

So my question is whether in the following code, File1.txt will properly be closed or if I have to close it manually. And if I have to do it manually, what happens if I don't?

std::fstream file("File1.txt");   
file = std::fstream("File2.txt"); //will this implicitly call file.close()?             
like image 745
MikeMB Avatar asked Feb 01 '16 20:02

MikeMB


People also ask

Does fstream automatically close?

When the fstream/ifstream/ofstream object is out of scope, the file will be closed automatically. However, you can explicitly call the close() function to close it and release the resource if the fstream object will not be out of scope for a while.

Does fstream need to be closed?

fstream is a proper RAII object, it does close automatically at the end of the scope, and there is absolutely no need whatsoever to call close manually when closing at the end of the scope is sufficient. In particular, it's not a “best practice” and it's not necessary to flush the output.

Does close flush C++?

NOTE: close() is typically called through the destructor of std::basic_filebuf (which, in turn, is typically called by the destructor of std::basic_fstream . First of all, we can see that it doesn't actually call flush() directly as you expected.

Do I need to close file C++?

There is no harm in closing it manually, but it's not the C++ way, it's programming in C with classes. If you want to close the file before the end of a function you can always use a nested scope. In the standard (27.8.


2 Answers

Move-assignment of an fstream object will result in move-assignment of its associated filebuf. The documentation for that makes it pretty clear that the old file is closed first (as-if file.rdbuf()->close() not file.close()):

basic_filebuf& operator=(basic_filebuf&& rhs);

  1. Effects: Calls this->close() then move assigns from rhs. After the move assignment *this has the observable state it would have had if it had been move constructed from rhs.
  2. Returns: *this.

basic_fstream& operator=(basic_fstream&& rhs);

  1. Effects: Move assigns the base and members of *this from the base and corresponding members of rhs.
  2. Returns: *this.

(This is the wording from draft n4527, unchanged since at least n3485)

like image 126
Ben Voigt Avatar answered Oct 24 '22 12:10

Ben Voigt


Since actual file-related machinery is 'hidden' inside corresponding buffers (streams really mostly provide formatting IO), you should be looking at std::basic_filebuf documentation:

First calls close() to close the associated file, then moves the contents of rhs into *this: the put and get buffers, the associated file, the locale, the openmode, the is_open flag, and any other state. After the move, rhs is not associated with a file and rhs.is_open() == false.

Copy from http://en.cppreference.com/w/cpp/io/basic_filebuf/operator%3D

like image 26
SergeyA Avatar answered Oct 24 '22 11:10

SergeyA