下面的代码是我同事的需求,是用C写的,我的任务是按照他的需求,在.net中写成二进制文件 typedef struct _timetable { long type; long time; // When type==IMMEDIATE, time is ignored; // When type==PERIODIC, time is calculate on-time long kind; union { struct { long basetime; long period; long lasttime;//UI need not consider this char cmdline[84]; } periodic; char cmdline[96]; } u; long nextstep;//Following char md5sum[16]; } TIMETABLE; ..... 我的代码是这样的: objFileStream = New FileStream( MapPath( "TB.dat" ), FileMode.Create ) objBinaryWriter = New BinaryWriter( objFileStream )
you should use specific types, not strings, seehttp://msdn.microsoft.com/library/en-us/cpguide/html/cpconstructssample.asp?frame=truehttp://msdn.microsoft.com/library/en-us/cpguide/html/cpconunionssample.asp?frame=true
typedef struct _timetable {
long type;
long time; // When type==IMMEDIATE, time is ignored;
// When type==PERIODIC, time is calculate on-time
long kind;
union {
struct {
long basetime;
long period;
long lasttime;//UI need not consider this
char cmdline[84];
} periodic;
char cmdline[96];
} u;
long nextstep;//Following
char md5sum[16];
} TIMETABLE;
.....
我的代码是这样的:
objFileStream = New FileStream( MapPath( "TB.dat" ), FileMode.Create )
objBinaryWriter = New BinaryWriter( objFileStream )
objBinaryWriter.Write( strType)
-objBinaryWriter.Write( strPeriod)
objBinaryWriter.Write( strClass)
objBinaryWriter.Write( strTime)
objBinaryWriter.Write( strBaseTime)
objBinaryWriter.Write( strCommand)
objBinaryWriter.Write( strNext)objBinaryWriter.Close但我没有考虑字节数问题,比如他要command要96个字节等。