Solved

Serialize: Appending to a file

Posted on 2000-03-14
7
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 10

Accepted Solution

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

Expert Comment

by:RONSLOW
ID: 2617789
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
ID: 2617792
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:trillo
ID: 2618036
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
ID: 2618311
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
ID: 2618314
oops that should have read

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

Author Comment

by:trillo
ID: 2620899
Thanks... Very Helpful
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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