高手救命 c#中如何读取二进制结构体文件:
文件结构体中有 :
unsigned short int Size;
char ID;
char Type;
short int IDS;
unsigned short int Date;
等数据类型
请问该怎么读取呀,小弟刚接触c#,求高手救命呀 。。。。
...全文
38310打赏收藏
高手救命 c#中如何读取二进制结构体文件?
高手救命 c#中如何读取二进制结构体文件: 文件结构体中有 : unsigned short int Size; char ID; char Type; short int IDS; unsigned short int Date; 等数据类型 请问该怎么读取呀,小弟刚接触c#,求高手救命呀 。。。。
/// <summary>
/// 读取二进制文件
/// </summary>
/// <param name="fileName">文件名</param>
public void ReadMyBinaryFile(string fileName)
{
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
/*
* public unsafe struct DATAPACKET
{
public fixed ushort head[6];
public ushort Size;
public byte ID;
public byte Type;
public short IDS;
public ushort Date;
}
*/
int iFileLength = fileStream.Length;//文件长度
int iStructLength = 20;//结构体的字节长度
int iTimes = iFileLength / iStructLength;//共有多少个字节
原来的结构是这样的 :
typedef struct{
unsigned short int head[6];
unsigned short int Size;
char ID;
char Type;
short int IDS;
unsigned short int Date;
}DATAP;
我已经把它转换为c#里面的不安全结构体了
如下:
public unsafe struct DATAPACKET
{
public fixed ushort head[6];
public ushort Size;
public byte ID;
public byte Type;
public short IDS;
public ushort Date;
}