Hi there!
In my lab I'm running a measuring program. It loops over taking data and writing it to disk. While the measuring is quite fast (1 kHz), the saving takes too long, especially since the file gets opened and closed every time before and after writing. So I thought I might open the file once, do all the writing on the open file, and close the file when I'm done. As a kind of "Autosave every 5 minutes" feature I wanted to implement it like "close and re-open the file after writing 1000 lines of data". So the code looks like this:
void CKlasseDlg::WriteDataToFile()
{
ofstream outfile;
if (m_iIndex == 1) {
//open file
outfile.open(m_sFile, ofstream::out | ofstream::app);
}
else if ((m_iIndex-1) % 1000 == 0) {//close and open file every 1000 lines to enforce writing of cached data to disk
outfile.close();
// open file
outfile.open(m_sFile, ofstream::out | ofstream::app);
}
// write
outfile << m_iIndex << "\t";
outfile << m_dDelta;
(...)
// append line feed
outfile << "\n";
// close file
outfile.close();
}
The problem is my file doesn't get beyond line 1, written when m_iIndex == 1. Apparently WriteDataToFile() uses a different instance of outfile every time it gets called. So I tried instantiating it once in CKlasseDlg::OuterLoop() (when I setup the file header anyway) and pass it on to WriteDataToFile. I changed CKlasseDlg.h to
void WriteDataToFile(ofstream &outfile);
, which yields a build error in MS Visual C++ 6.0: "error C2061: Syntax error : Identifier 'ofstream' Can't find overloaded member function 'void (class ofstream &)' in 'CKlasseDlg' - see declaration of 'CKlasseDlg.h'"
What keeps puzzling me is that this ofstream thingy doesn't seem to be an ordinary variable. Is there a better way to pass some kind of file handle to WriteDataToFile()?
Regards,
Rufus