samedi 19 octobre 2019

Why close() is not called automatically when going out of scope while using std::shared_ptr

We know that when we use shared_ptr the destructor will be call automatically when it goes out of scope. But in the case of fStream same thing should happen. But it is not happening like this. We want to use close() function to call the destructor of fStream. File is not closing automatically without using the close().

#include <memory>
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
  std::shared_ptr<std::fstream> file = std::make_shared<std::fstream>(); 
  file->open("test.txt", std::fstream::in | std::fstream::out | 
                                            std::fstream::app);
  file->put('s');
  file->close();
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire