将二进制文件读入struct但得到全0?

时间:2016-03-08 16:50:50

标签: c# delphi

我正在将delphi程序转换为c#,我必须将二进制文件中的数据读入struct。我在此问到的上一个问题回答了一种方法:Proper struct layout from delphi packed record

然而,当尝试使用我拥有的另一个结构复制它时,这很有效,即使文件中实际存在数据,所有数据都是0。该文件是根据这些结构(或delphi版本中的打包记录)编写的,这个特定的代码应该迭代到文件中的最后一个'struct',并将其读入。我是将列出delphi和c#代码。

的Delphi:

CONST
   RecordSize=128;
   HeaderSize=128;

Testrec = packed record
     now: TDateTime;
     ShLat: longint;
     ShLong: longint;
     ShLatUn: short;
     ShLonUn: short;
     ShSens1: short;
     ShSens2: short;
     ShAlt: single;
     ShFirst: single;
     ShDepth: single;
     ShSpeed: single;
     ShHead: single;
     ShCourse: single;
     ShRoll: single;
     ShPitch: single;
     ShVOS: single;
     FiLat: longint;
     FiLong: longint;
     FiLatUn: short;
     FiLonUn: short;
     FiSens1: short;
     FiSens2: short;
     FiAlt: single;
     FiFirst: single;
     FiDepth: single;
     FiSpeed: single;
     FiHead: single;
     FiCourse: single;
     FiRoll: single;
     FiPitch: single;
     FiVOS: single;
     DataFlags: longint;
     RevFlags: longint;
     Contact: longint;
     Range1: short;
     Range2: short;
     end;

var
  RampStream:TFileStream;

Function GetLastTime:TDateTime;
var
  RRampRec:TestRec;
  LastPosition:TDateTime;
  HoldPosition:Int64;
begin
  LastPosition:=0;
  HoldPosition:= RampStream.Position;
  RampStream.Position:=128;
  While RampStream.Position < RampStream.Size do
  begin
    RampStream.Read(RRampRec,RecordSize);
    if (RRampRec.ShLat = 0) AND (RRampRec.ShLon = 0)  AND
    (RRampRec.FiLat = 0) AND (RRampRec.FiLon = 0)  then  continue;
    LastPosition:= RRampRec.Now;
  end;
  RampStream.Position:=HoldPosition;
  GetLastTime:=Lastposition;  
end;

C#:

const int RecordSize = 128;
const int HeaderSize = 128;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Testrec
{
    public double now;
    public int ShLat;
    public int ShLon;
    public short ShLat;
    public short ShLon;
    public short ShSens1;
    public short ShSens2;
    public float ShAlt;
    public float ShFirst;
    public float ShDepth;
    public float ShSpeed;
    public float ShHead;
    public float ShCourse;
    public float ShRoll;
    public float ShPitch;
    public float ShVOS;
    public int FiLat;
    public int FiLon;
    public short FhLatUn;
    public short FhLonUn;
    public short FhSens1;
    public short FhSens2; 
    public float FhAlt;
    public float FhFirst;
    public float FhDepth;
    public float FhSpeed;
    public float FhHead;
    public float FhCourse;
    public float FhRoll;
    public float FhPitch;
    public float FhVOS;
    public int DataFlags;
    public int RevFlags;
    public int Contact;
    public short Range1;
    public short Range2;
}

FileStream RampStream;

private Double GetLastTime()
{
    Testrec RRampRec = new Testrec();
    double LastPosition;
    long HoldPosition;

    LastPosition = 0;
    HoldPosition = RampStream.Position;
    RampStream.Position = 128;

    while (RampStream.Position < RampStream.Length)
    {
        RRampRec = ReadRecFromStream2(RampStream);
        if ((RRampRec.ShLat == 0) && (RRampRec.ShLong == 0) && (RRampRec.FiLat == 0) && (RRampRec.FiLon == 0))
        {
            LastPosition = RRampRec.now;
        }
    }
    RampStream.Position = HoldPosition;
    return LastPosition;
}

TestrecReadRecFromStream2(Stream stream)
{
    byte[] buffer = new byte[Marshal.SizeOf(typeof(Testrec))];
    stream.Read(buffer, 0, HeaderSize);
    GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
    try
    {
        return (Testrec)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Testrec));
    }
    finally
    {
        handle.Free();
    }
}

注意:流在前一个结构(即文件的第一个结构)中读取时已位于第128位。所以RampStream.Position = 128开始这些功能时。

知道这里可能出现什么问题吗?我可以验证流的位置一次正确地上升128个字节。另外,我可以在RampStream.Position的末尾验证RampStream.Length是否等于while loop

1 个答案:

答案 0 :(得分:2)

Delphi代码的缩进似乎让您感到困惑。它应该像这样缩进:

while RampStream.Position < RampStream.Size do
begin
  RampStream.Read(RRampRec,RecordSize);
  if (RRampRec.ShLat = 0) and (RRampRec.ShLon = 0) 
     and (RRampRec.FiLat = 0) and (RRampRec.FiLon = 0) then  
    continue;
  LastPosition := RRampRec.Now;
end;

因此,LastPosition仅在if条件为假时分配。 LastPosition条件为真时,您的C#代码会分配if。要使C#代码匹配,您可以写:

while (RampStream.Position < RampStream.Length)
{
    RRampRec = ReadRecFromStream2(RampStream);
    if ((RRampRec.ShLat == 0) && (RRampRec.ShLong == 0) 
       && (RRampRec.FiLat == 0) && (RRampRec.FiLon == 0))
        continue;
    LastPosition = RRampRec.now;
}

如果是我,虽然我会避免使用continue并写下这样的样子:

while (RampStream.Position < RampStream.Length)
{
    RRampRec = ReadRecFromStream2(RampStream);
    if ((RRampRec.ShLat != 0) || (RRampRec.ShLong != 0) 
       || (RRampRec.FiLat != 0) || (RRampRec.FiLon != 0))
        LastPosition = RRampRec.now;
}

我总是对单个/复合语句感到紧张所以我可能会添加大括号:

while (RampStream.Position < RampStream.Length)
{
    RRampRec = ReadRecFromStream2(RampStream);
    if ((RRampRec.ShLat != 0) || (RRampRec.ShLong != 0) 
       || (RRampRec.FiLat != 0) || (RRampRec.FiLon != 0))
    {
        LastPosition = RRampRec.now;
    }
}