Solved

Serialize: Appending to a file

Posted on 2000-03-14
7
263 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 62
how to monitor remote shell execution on linux 9 97
How to determine file rename from EventLog ? 2 42
bigDiff challenge 17 84
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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now