Bill H
Bill H

Reputation: 9

How can I get the size of the file referenced in a CArchive?

I have just started to get back into c++ after years using Perl, php and assembler and I am trying to create a simple MFC program using Visual Studio 2017 and c++ to open binary files for viewing. I am trying to work within the code created by the wizard and I have gotten stumped. I know this is probably not the best way of doing what I want to do but I am learning.

Anyways the code I am working on is:

void CAdamImageManagerDoc::Serialize(CArchive& ar)
{
    if (ar.IsStoring())
    {
        // TODO: add storing code here
    }
    else
    {
        // TODO: add loading code here
        char documentBuffer[1000];
        ar.Read(documentBuffer, 1000);

        AfxMessageBox((LPCTSTR)documentBuffer);
    }
}

This is called after you select a file using the standard mfc file open dialog box OnFileOpen. What I am trying to figure out is:

  1. how can I know what the size is of the file that was referenced in the call?
  2. how can I find out what the name of the file referenced is?

This is my first question on here in almost 10 years so please be gentle and not tell me how I didn't format the question properly or other things.

Upvotes: 0

Views: 415

Answers (2)

xMRi
xMRi

Reputation: 15365

In general you decode the stream of a CArchive in the reverse ways like you write it.

So in most cases there is no need to know the size of the file. Serializing n elements is mostly done using CObList or CObArray or you just write the size of a data block into the archive followed by the BYTEs. The same way you may decode the stream.

if (ar.IsStoring())
{
    DWORD dwSize = m_lenData;
    ar << dwSize;
    ar.Write(documentBuffer, dwSize);
}
else
{
    DWORD dwSize;
    ar >> dwSize;
    ar.Read(documentBuffer, dwSize);
}

If you look at the MFC code how a CString is serialized or how a CObArray is serialized you find the same way.

Note that in this case the file turns into a binary file. And is no longer just "text".

Upvotes: 2

Jabberwocky
Jabberwocky

Reputation: 50831

  • Use ar.GetFile()->GetFilePath() to get the complete file path to the file (reference)
  • Use ar.GetFile()->GetLength() to get the file size. (reference)

Upvotes: 4

Related Questions