Hello, at the moment I am writing a program which needs to open, close and modify many different files at the same time (typically over 100 at a given time). Because of this requirement of the project, I decided to make a class which contains a vector of pointers to the different fstream instances for the files. That way I have a simple and 'clean' way to handle this many files. The fact that I am using vectors also means I can easily add new fstream instances (open new files) just by using the push_back method etc.
For some reason, I'm having a bit of difficulty getting it to open a new file (it shouldn't be this hard). The program compiles it just doesn't do what I want it to, and I can't work out where the problem is.
Code:#include <fstream> #include <vector> #include <string.h> using namespace std; class CFileManage { private: CFileManage(); virtual ~CFileManage(); vector<fstream *> m_file; static CFileManage * hcInstance; public: /* Make sure only one instance of this class is generated */ static CFileManage * GetClassInstance(); static void GenerateClassInstance(); void DeleteClassInstance(); /* Make use of the i/o filestream vectors */ bool CreateNewFileInstance(char * filename); void CreateNewFileInstance(string filename); void CloseFileInstance(unsigned int vec_idx); void CloseAllFileInstances(); unsigned int GetNumberOfFileInstances(); fstream * GetFileInstance(unsigned int vec_idx); };For some reason, the code compiles, but it doesn't work as expected when it comes to making a new file.Code:bool CFileManage::CreateNewFileInstance(char * filename) { m_file.push_back( new fstream ); m_file.back()->open(filename); if( m_file.back()->is_open() ) { return false; } return true; } void CFileManage::CloseAllFileInstances() { int idx; for(idx=0; idx < m_file.size(); idx++) { m_file[idx]->close(); delete m_file[idx]; } m_file.clear(); return; }
At a later date I plan to change the CreateFile Methods so that it allows support for binary files aswell as different modes, e.g. app, ate, trunc etc.



LinkBack URL
About LinkBacks



