如何把一个结构体写入文件
怎么把一个结构体写入文件?
这样的结构体
pAddress1/pAddress2/pText指向一个不定的字符串长度,当一个这样的结构体的变量pItem存在时,也就指针指向的字符串也都确定了,我想用cfile file.write把它写入本地一个文件中,并且能再从这个文件中读取把这个结构体还原,求写入和读取的方法,在unicode环境下
------解决方案--------------------
楼主的主要疑问在于指针成员如何保存和读取:
TItem pItem ;
....
pItem.pAddress1=_T("12222");
....
pItem.pText = _T("3333218dddd22222222...");
如果结构体已经固定,不能变化,可行的方法之一是:
typedef struct
{
TIndex nIndex;
TType type;
unsigned short *pAddress1;
TFolder folder;
unsigned short *pAddress2;
unsigned short *pText;
//。。。。。。实际结构体比这复杂很多
}TItem;
writefile( &pItem.nIndex,sizeof( nIndex ) );
writefile( &pItem.type,sizeof type );
int len_of_addr1 = wstrlen( pAddress1 );
writefile( &len_of_addr1,4 );
writefile( pItem.pAddress2,len_of_addr1);
.....
这样的结构体
- C/C++ code
typedef struct { TIndex nIndex; TType type; unsigned short *pAddress1; TFolder folder; unsigned short *pAddress2; unsigned short *pText; //。。。。。。实际结构体比这复杂很多 }TItem; TItem pItem ; .... pItem.pAddress1=_T("12222"); .... pItem.pText = _T("3333218dddd22222222...");
pAddress1/pAddress2/pText指向一个不定的字符串长度,当一个这样的结构体的变量pItem存在时,也就指针指向的字符串也都确定了,我想用cfile file.write把它写入本地一个文件中,并且能再从这个文件中读取把这个结构体还原,求写入和读取的方法,在unicode环境下
------解决方案--------------------
楼主的主要疑问在于指针成员如何保存和读取:
TItem pItem ;
....
pItem.pAddress1=_T("12222");
....
pItem.pText = _T("3333218dddd22222222...");
如果结构体已经固定,不能变化,可行的方法之一是:
typedef struct
{
TIndex nIndex;
TType type;
unsigned short *pAddress1;
TFolder folder;
unsigned short *pAddress2;
unsigned short *pText;
//。。。。。。实际结构体比这复杂很多
}TItem;
writefile( &pItem.nIndex,sizeof( nIndex ) );
writefile( &pItem.type,sizeof type );
int len_of_addr1 = wstrlen( pAddress1 );
writefile( &len_of_addr1,4 );
writefile( pItem.pAddress2,len_of_addr1);
.....