c++ - std::ifstream closed for some reason? -


i'm trying open ifstreams contain file data, can read later on. i'm trying pass vector of ifstreams constructor, reason when loop through vector, references closed. but, other variables (data stream , meta stream) remain open.

what missing here? i'm coming heavy java background, i'm still learning c++

filestore.h

// // created tom on 8/16/2017. //  #ifndef cfs_filestore_h #define cfs_filestore_h  #include <fstream> #include <list> #include "bytebuffer.h"  class filestore {  private:     std::ifstream *data_stream;     std::vector<std::ifstream *> index_streams;     std::ifstream *meta_stream;  public:     filestore(std::ifstream *data, std::vector<std::ifstream *> indexes, std::ifstream *meta);     ~filestore() = default;      int get_type_count();     bytebuffer read(int type, int id);     int get_file_count(int type);      static filestore open(std::string &root); };   #endif //cfs_filestore_h 

filestore.cpp

// // created tom on 8/16/2017. //  #include "filestore.h"  filestore::filestore(std::ifstream *data, std::vector<std::ifstream *> indexes, std::ifstream *meta)         : data_stream(data), index_streams(std::move(indexes)), meta_stream(meta) {      std::cout << std::boolalpha << "data open : " << data_stream->is_open() << std::endl;     std::cout  << "meta open : " << meta_stream->is_open() << std::endl;      (auto v : index_streams) {         std::cout << "index open : " << v->is_open() << std::endl;     } }  int filestore::get_type_count() {     return 0; }  bytebuffer filestore::read(int type, int id) {     return bytebuffer(); }  int filestore::get_file_count(int type) {     return 0; }  filestore filestore::open(std::string &root) {     std::ifstream data(root + "main_file_cache.dat2");     if (!data.good())         throw std::runtime_error("data file not exist.");      std::vector<std::ifstream *> indexes;     (int = 0; < 254; i++) {         std::ifstream index(root + "main_file_cache.idx" + std::to_string(i));         if (!index.good())             break;          indexes.push_back(&index);     }      std::ifstream meta(root + "main_file_cache.idx255");     if (!meta.good())         throw std::runtime_error("meta file not exist.");      return filestore(&data, indexes, &meta); } 

output

data open : true meta open : true index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false index open : false 

    std::ifstream index(root + "main_file_cache.idx" + std::to_string(i)); 

this constructs std::ifstream object. gets declared automatic scope.

    indexes.push_back(&index); } 

this pushes pointer std::ifstream indexes vector, because index's automatic scope ends afterwards, index object gets destroyed, corresponding file being closed. declared std::ifstream object in automatic scope, inside inner loop. such, object gets destroyed @ end of loop.

the subsequent code attempts dereference these pointers, stored in indexes vector, dangling pointers. results in undefined behavior.

additionally, indexes vector gets passed value, results in vector getting copied, adds confusion.

you need re-read following chapters in c++ book:

  • the chapter explains how automatic scope, , how dynamic scope works in c++.

  • the chapter explains difference between passing function arguments reference , passing them value, , means.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -