我有一个叫TOWRITE的建筑。我想把结构的内容写到一个文件中.但有一件事是结构成员的大小是不固定的。它取决于从其他来源收到的数据。有没有可能用一种简单的方式写出结构。请参考下面的代码片段。
谢谢
typedef struct TOWRITE
{
DWORD dwHeader;
WORD datacount;
LPBYTE lpData;
WORD checksum;
}
TOWRITE towrite;
ZeroMemory( &towrite, sizeof( TOWRITE ));
towrite.lpData = (LPBYTE) new BYTE[256];
towrite.dwHeader = 0xF1F1E1E1;
towrite.datacount = 256;
towrite.cheksum = 3849;
CopyMemory( towrite.lpData, szTemp, 256 );
.....
.....
.....
f.write( (LPBYTE)&towrite, sizeof(TOWRITE) );
....
....
....
....发布于 2012-08-25 19:52:19
typedef struct TOWRITE
{
DWORD dwHeader;
WORD datacount;
WORD checksum;
LPBYTE lpData; //Put the ptr at the end
}
// - sizeof(LPBYTE) avoid the ptr @ to be written
f.write( (LPBYTE)&towrite, sizeof(TOWRITE) - sizeof(LPBYTE));
// write the ptr data.
// sorry I don't know windows C.
// fix the size of the array element :)
f.write( towrite.lpData, towrite.datacount * sizeof(????)); https://stackoverflow.com/questions/12121602
复制相似问题