Solved

Serialize: Appending to a file

Posted on 2000-03-14
7
253 Views
Last Modified: 2013-11-20
I need to save the contents of my document on a file which already has data, this means I must append the data to a file.
How ever, on the document::Serialize function, I use something like this:

void CMyDoc::Serialize(CArchive& ar)
{
// Assume is storing here
    CFile *pFile= ar.GetFile();
    pFile->SeekToEnd();
    ...
    pEditView->SerializeRaw(ar);
}

But it doesn't work, the file is always overwritten... What's wrong???
0
Comment
Question by:trillo
  • 5
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
Comment Utility
The file is already deleted and recreated by the time you get to the serialize.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Also, appending to a file saved with serialize (via CArchive) will not work (that easily), as you will end up writing the file header twice.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
What you could do is create a temporary CMyDoc object, serialize in from the file you wish to append to, apppend the info from the real CMyDoc object and then serialize out the combined file.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:trillo
Comment Utility
Oh!... And can avoid using the Serialize function.... What if I used CFile directly... will it be the same?
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Actually, if you override the CMyDoc::GetFile so that, if opening for read, it also opens for append, then you should be ok.

eg.

CFile* CMyDoc::GetFile(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError) {
  bool bWriting = CFile::modeCreate == (nOpenFlags & CFile::modeCreate)
  if (bWriting) {
    nOpenFlags |= CFile::modeNoTruncate;
  }
  CFile* pFile = CDocument::GetFile(lpszFileName,nOpenFlags,pError);
  if (bWriting && pFile) {
    pFile->SeekToEnd();
  }
  return pFile;
}

This should ensure that you are appending to the existing file all the time.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
oops that should have read

....if opening for WRITE, it also opens for append, then...
0
 
LVL 2

Author Comment

by:trillo
Comment Utility
Thanks... Very Helpful
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sum28 challenge 31 95
zeroFront challenge 7 70
sumHeights  challenge 17 59
Problem to event 3 47
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now