Click here to Skip to main content
15,899,547 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have searched the documentation on this site and can not find an answer for converting a file from my old format to the new format to include a new member added to my array. If I add a member to my array, compile & run, my program tells me the file it's trying to open has an incorrect file format. Can anyone point me in a direction to learn how to update my files after adding the new array member?

Thanks for having my back!

DrB
Posted
Updated 28-Jul-11 9:42am
v3
Comments
Albert Holguin 28-Jul-11 15:27pm    
You're going to have to clarify, arrays don't have members. Not sure what you mean, are you using a specific class or library? Post some code with your question.
DrBones69 28-Jul-11 16:42pm    
I have a class with members, I am storing the members in a CArray, if I add a member to the class the program doesn't recognize my previous stored file. It says "unexpected file format". I can create a new file with the new member, but I want to be able to reformat my previous file with the new format of my class.

You have to learn how to use file headers.
Example:
typedef struct
{
  char          magic[4];
  unsigned int  cbheader;
  unsigned int  version; enum{ CURRENTVERSION=1, };
  unsigned int  array_count;

} MYFILEHEADER;

#define  MYMAGIC  "abcd"

void writemyfile(IStream* pstream,int* array,const unsigned int count)
{
  MYFILEHEADER  h = { MYMAGIC, sizeof(MYFILEHEADER), MYFILEHEADER::CURRENTVERSION, };
  unsigned long  w;
  h.array_count = count;
  stream->Write(&h,sizeof(h),&w);
  stream->Write(array,sizeof(int)*count,&w);
}
unsigned int readmyfile(IStream* pstream,int** array)
{
  MYFILEHEADER  h;
  unsigned long  r;

  *array = 0;
  if(S_OK!=stream->Read(&h,sizeof(h),&r)) || (sizeof(h)!=r)) return 0;
  if((h.cbheader!=sizeof(h)) || memcmp(MYMAGIC,h.magic,sizeof(h.magic))) return 0;
  if(h.version!=MYFILEHEADER::CURRENTVERSION) return 0; // or do an alternate handling
  *array = malloc(sizeof(int)*h.array_count);
  if(  (S_OK!=stream->Read(*array,sizeof(int)*h.array_count,&r)) ||
      ((sizeof(int)*h.array_count)!=r))
  {
    free(*array); *array = 0; return 0;
  }
  return h.array_count;
}

Good luck.
 
Share this answer
 
Comments
DrBones69 29-Jul-11 20:53pm    
Thanks for the info, but I think that's a little much for what I'm trying to accomplish right now. It is something that I will do in the future.

DrB
MFC is reporting an incongruence between the save file and the reading functions, that, in your new version, pretend to read data that aren't there.

In general to avoid this, you should manage a proper "version number" (and read the data coherently to the version), or go through a "conversion program", that reads the data in the old way and rewrite it in the new way.
 
Share this answer
 
Wouldn't it be easier to adjust the serialization of the class members to read in the old file format and then save the new format to the same file. Like this:

C++
void CCustomers::Serialize(CArchive& ar)
{
    CObject::Serialize(ar);
    
	if (ar.IsStoring())
	{
        ar << m_sName;
        ar << m_sLastName;
        ar << m_sAddress;
        ar << m_sCity;
        ar << m_sStCombo;
        ar << m_sZip;
        ar << m_sNEWMEMBER;
        }
        else
        {
        ar >> m_sName;
        ar >> m_sLastName;
        ar >> m_sAddress;
        ar >> m_sCity;
        ar >> m_sStCombo;
        ar >> m_sZip;
//      ar >> m_sNEWMEMBER;
        }   


Comment out the new member to allow the old format and then save with the new member in the output. And then you can remove the comment for the read in on the new member.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900