Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Serialize: Appending to a file

Posted on 2000-03-14
7
Medium Priority
?
321 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 200 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
Technology Partners: 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

604 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