向数组添加成员并更新我的文件 [英] Adding a member to an array and updating my file

查看:79
本文介绍了向数组添加成员并更新我的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了该站点上的文档,但找不到将文件从旧格式转换为新格式以包括添加到阵列中的新成员的答案.如果我将一个成员添加到我的数组中,然后编译并运行,我的程序会告诉我它试图打开的文件的文件格式不正确.任何人都可以指出我的方向,以学习添加新的数组成员后如何更新文件吗?

感谢您的支持!

DrB

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

推荐答案

您必须学习如何使用文件头.
示例:
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.


MFC报告了保存文件和读取功能之间的不一致,在您的新版本中,MFC假装读取不存在的数据.

通常,为避免这种情况,您应该管理适当的版本号"(并一致地读取数据),或者通过转换程序",该程序以旧的方式读取数据并以新的方式重写数据. .
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.


调整类成员的序列化以使其以旧文件格式读取,然后将新格式保存到相同文件中会更容易.像这样:

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:

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.


这篇关于向数组添加成员并更新我的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆