Click to See Complete Forum and Search --> : CArchive question


Thomas
March 30th, 1999, 01:57 AM
Hi there,

i have the followin portion of code which works fine for me,

but there is 1 question left.

first letīs look at the code

CView::AnyFunction

{

CFile file;

CFileException fe;

file.Open(m_strFileName, CFile::modeCreate|CFile::modeWrite, &fe);

CArchive ar(&file, CArchive::store);

ar.m_pDocument = GetDocument();

SerializeRaw(ar);

}

As i noticed, the code works, the file in my edit ctrl is stored.

The question is: Do I have to have a call to file.Close() and if yes where?

I tried to Clode the handle after 'SerializeRaw(ar)' but this cuases a crash

because the Write - File function seems to be called later.

Can anybody tell me a couple of things on this?!?


thanks very much

Dave Lorde
March 30th, 1999, 06:00 AM
You don't have to close the file, because the CFile destructor will do that for you when the file goes out of scope.


Closing it in your function didn't work because -


The CArchive docs say:


"You may not use CFile operations to alter the state of the file until you have closed the archive. Any such operation will damage the integrity of the archive."


"CArchive::Close() flushes any data remaining in the buffer, closes the archive, and disconnects the archive from the file. No further operations on the archive are permitted. After you close an archive, you can create another archive for the same file or you can close the file."


"The member function Close ensures that all data is transferred from the archive to the file, and it makes the archive unavailable. To complete the transfer from the file to the storage medium, you must first use CFile::Close and then destroy the CFile object."


The CArchive::Close function will be called from the CArchive destructor, which is run when CArchive goes out of scope - at the end of your function, unless you call it explicitly before then.


Hope that helps,


Dave

June 21st, 2000, 06:53 PM
You have to ar.Close(); and file.Close(); right after SerializeRaw(ar);.

arnie
June 23rd, 2000, 06:50 PM
The best way to do this is:
UpdateData(TRUE);// Move the what is in edit ctrl to m_pDocument
CFile file;
file.Open(m_strFileName,CFile::modeCreate|CFile::modeWrite);
CArchive ar(&file,CArchive::store);
ar<< m_pDocument; // m_pDocument attached to edit ctrl
ar.Close();
file.Close();

This will save what you have in the edit ctrl to a file.