【发布时间】:2016-08-11 13:38:28
【问题描述】:
我正在尝试将旧的 Delphi 应用程序转换为 C#。它通过packed records 编写的binary files 做了一些事情,我把它们放在下面。但是,只有BInfoRec record 才能保证在文件中并首先出现。其他人可能在也可能不在,他们的顺序是未知的。特别是我在使用一种方法时遇到了麻烦。它获取通过FileStream.Read 读取的字节数并将这些字节数读入packed records 之一。然后,在第一个if 语句中(在delphi 版本中),它在堆上分配内存,并且做和以前一样的事情,但是将它读入一个指针。我正在尝试找出解决此问题的最佳方法,但我绝不是 Delphi 的专家。
德尔福代码:
StartP = packed record
x:SmallInt;
y:SmallInt;
end;
InfoP = packed record
Ycoord:double;
Xcoord:double;
//other vars here
end;
HeadP = packed record
NumP:DWORD;
SizeStruct:DWORD;
SizePoStruct:DWORD;
//other vars here
end;
BInfoRec = packed record
StructNum : WORD ;
in_size : WORD ;
//other variables here
end;
var
tStream:TFileStream;
bInfo:BInfoRec;
RestOfBFile:Pointer;
sizeofRest:Integer;
Function LoadBFile(FileName:String):Boolean;
var
sizeread:Integer;
begin
Try
LoadBFile:=False;
tStream:=TFileStream.Create(Filename,fmOpenRead );
sizeofRest:=tStream.Size-Sizeof(bInfo);
sizeread:=tStream.Read(bInfo,Sizeof(bInfo));
if sizeread = Sizeof(bInfo) then
begin //best way to convert this?
RestOfBFile:=AllocMem(sizeofRest);
sizeread:=tStream.Read(RestOfBFile^,sizeofRest);
if SizeofRest= SizeRead then
LoadBFile:=True;
end;
tStream.Free;
except
LoadBFile:=False;
tStream.Free;
end;
end;
C#(我目前拥有的):
[Serializable()]
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct StartP
{
public short x;
public short y;
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct InfoP
{
public double Ycoord;
public double Xcoord;
//other vars here
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct HeadP
{
public UInt32 NumP;
public UInt32 SizeStruct;
public UInt32 SizePoStruct;
//other vars here
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct BInfoRec
{
public ushort StructNum;
public ushort in_size;
}
BInfoRec bInfo;
int sizeOfRest;
private Boolean LoadBFile(string fileName)
{
int sizeRead;
byte[] buffer = new byte[Marshal.SizeOf(bInfo)];
try
{
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
sizeOfRest = (int)stream.Length - Marshal.SizeOf(typeof(BInfoRec));
sizeRead = stream.Read(buffer, 0, Marshal.SizeOf(typeof(BInfoRec)));
if (sizeRead == Marshal.SizeOf(typeof(BInfoRec)))
{
//what goes here??
if (sizeOfRest == sizeRead)
{
return true;
}
}
}
}
catch (Exception ex)
{
return false;
}
}
我正在考虑创建一个未知大小的新字节数组并使用BinaryReader 将文件的其余部分读入该数组,然后只检查它的大小。不确定这是否是最好的方法?
【问题讨论】:
标签: c# bytearray delphi-7 filestream