In the c++ language, there are multiple ways how to open and operate on a file. However the RAII-approach is very popular, since the destruktor takes care about freeing memory.
But what about the closing of a filestream? As far as i know, the destruktor should close the file anyway. However the destruktor also isn't allowed to throw any exceptions. So if i don't close my stream and it gets destructed at the end of a block i might loose information about the error, which might occur while closing.
Is my thinking correct? Despite of the RAII approach i should always close my streams?
Example:
{
std::ofstream ofs;
ofs.open ("test.txt");
ofs << "blablabla";
//do i need the following?
ofs.close();
}
.close()
, that's the whole point of RAII. – Perspicacious