2

I have a code in which CArchive is being used to read and write and file. Upon my investigation I found out that CArchive object changes its location when reading data from different parts of file. For example, if file structure is like there is header, then body and then footer. Now if someone want to read footer then CArchive reads footer only by going to specific location in file. This is done by this.

COleStreamFile stream;
//Stream is pointed to footer location.
stream.OpenStream(m_pStg, "Footer", nOpenFlags, pError);  // pStg is LPSTORAGE
CArchive ar(&stream, CArchive::load);

Now what I am interested in knowing is that at which location CArchive is going to read or write. Byte index, file location or something like that.

fhnaseer
  • 7,159
  • 16
  • 60
  • 112

1 Answers1

3

You cannot get the position from the CArchive object directly, but you can obtain it from the underlying stream - COleStreamFile in your case. Simply call CFile::GetPosition.

Example:

COleStreamFile stream;
//Stream is pointed to footer location.
stream.OpenStream(m_pStg, "Footer", nOpenFlags, pError);  // pStg is LPSTORAGE
CArchive ar(&stream, CArchive::load);
// Add some data.
ar << someData;

// And get the current position.
int currentPos = stream.GetPosition();
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • one question, if I Do int poistion1 = stream.GetPosition(); ar << someData; int position2 = stream.GetPosition(); position2 will be different than position1, right?, – fhnaseer May 28 '13 at 12:16
  • I assume so, but I don't have a computer with Visual Studio installed right now. You better test it yourself - add this code and debug it, and see what happens. – sashoalm May 28 '13 at 12:17