c++11 - Filewrite not happening when ofstream pointer is in a std::map in C++ -


wrt below code, compiles fine & executes without exception. no content gets written javascript file. it's 0 bytes.

std::map<std::string, std::shared_ptr<std::ofstream>> m_jstabfilesmap; m_jstabfilesmap.insert({ filekey, std::make_shared<std::ofstream>(jsfilename) }); auto jsfile = m_jstabfilesmap[tabname]; (*jsfile).open(tabname + ".js"); *jsfile << contentofjsfile;  // write js file 

not sure did miss, how make write file.

[updates question] using normal pointer method causing same result.

std::map<std::string,std::ofstream*> m_jstabfilesmap; m_jstabfilesmap.insert({ filekey, new std::ofstream(jsfilename)}); 

also tried removing explicit open() call & same result, nothing gets written file.

writing files may buffered performance (by c++ lib, underling c lib, os, etc., etc.) because storage devices can not directly write few bytes @ time, entire sectors (often 4096 or 512 bytes). data may not written or "visible" until buffers "flushed". happen when file closed or if buffer full (quite possibly many kb). if storing in map may not waiting closed before checking?

to flush file explicitly std::ostream::flush method. remember there performance penalty doing so.

also should check errors. c++ streams dont default throw exceptions in cases, if opening file fails, want check error state (e.g. good() or fail()). allthough if implied code works normal stack allocated stream, version using stored map should work.


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 -