有一个简单的二进制文件,保存着一些配置信息,用文本编辑器打开都能看到
有8个配置项,值也能看到。我想这样的文件格式因该不难分析吧,只是没有做过这样的工作
我不知道能不能把这个文件结构用delphi的record给表示出来,便于我读写,分析了一下还是没有思路希望做过这方面的朋友帮忙看一下http://xtoolosft.32.7data.com/1.rar

解决方案 »

  1.   

    unit Unit5;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls,RecordUnit;type
      TForm5 = class(TForm)
        GroupBox1: TGroupBox;
        Button1: TButton;
        Button2: TButton;
        Edit1: TEdit;
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        Label4: TLabel;
        Label5: TLabel;
        Edit2: TEdit;
        Edit3: TEdit;
        Edit4: TEdit;
        ComboBox1: TComboBox;
        DateTimePicker1: TDateTimePicker;
        Button3: TButton;
        procedure Button3Click(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        PersonRecord:TPersonRecord;
        RecordStream:TRecordStream;
        Procedure ShowRecord;
      public
        { Public declarations }
      end;var
      Form5: TForm5;implementation{$R *.dfm}procedure TForm5.Button1Click(Sender: TObject);
    begin
      with PersonRecord do
      begin
        FirstName:=edit2.Text;
        LastName:=edit3.Text;
        Sex:=Combobox1.ItemIndex;
        BirthDay:=FormatDateTime('yyyy-MM-dd',DateTimePicker1.DateTime);
        Age:=StrToInt(edit4.Text);
      end;
      RecordStream.AppendRecord(PersonRecord)
    end;procedure TForm5.Button2Click(Sender: TObject);
    begin
      if RecordStream.RecordsCount>0 then
        RecordStream.FirstRecord;
      ShowRecord;
    end;procedure TForm5.Button3Click(Sender: TObject);
    begin
      if FileExists(edit1.Text) then
        RecordStream:=TRecordStream.Create(edit1.Text,fmOpenReadWrite)
      else
        RecordStream:=TRecordStream.Create(edit1.Text,fmCreate);
    end;procedure TForm5.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      RecordStream.Free;
    end;procedure TForm5.ShowRecord;
    begin
      RecordStream.ReadRecord(PersonRecord);
      with PersonRecord do
      begin
        edit2.Text:=FirstName;
        edit3.Text:=LastName;
        Combobox1.ItemIndex:=Sex;
        DateTimePicker1.DateTime:=StrToDateTime(BirthDay);
        edit4.Text:=IntToStr(Age);
      end;
    end;end.
      

  2.   

    unit RecordUnit;interface
    uses Classes,SysUtils;
    Type
      TPersonRecord=Packed Record
        FirstName:String[10];
        LastName:String[10];
        Sex:Integer;
        BirthDay:String[10];
        Age:Integer;
      End;  TRecordStream=Class(TFileStream)
      Private
        Function GetRecordsCount:LongInt;//总记录数
        Function GetCurRecords:LongInt;
        Procedure SetCurRecords(RecordNum:LongInt);
      Protected
        Function GetRecordSize():LongInt;Virtual;
      Public
        Function SeekRecord(RecordNum:LongInt;Origin:Word):LongInt;
        Function WriteRecord(Const _Record:TPersonRecord):LongInt;
        Function AppendRecord(Const _Record:TPersonRecord):LongInt;
        Function ReadRecord(var _Record:TPersonRecord):LongInt;
        Procedure FirstRecord;
        Procedure PreviousRecord;
        Procedure NextRecord;
        Procedure LastRecord;
        Property RecordsCount:LongInt Read GetRecordsCount;
        Property CurRecord:LongInt Read GetCurRecords Write SetCurRecords;
      End;
    implementation{ TRecordStream }function TRecordStream.AppendRecord(const _Record:TPersonRecord): LongInt;
    begin
      Seek(0,2);//??
      Result:=Write(_Record,GetRecordSize);//Write(要写入的内容,内容的最大大小)
    end;procedure TRecordStream.FirstRecord;
    begin
      Seek(0,0);
    end;function TRecordStream.GetCurRecords: LongInt;
    begin     //Position当前的偏移量
      Result:=(Position div GetRecordSize)+1;//??
    end;function TRecordStream.GetRecordsCount: LongInt;
    begin
      Result:=Size div  GetRecordSize;
      {
        这里的Size是流文件的大小,以bytes形式
        用文件大小除以记录大小得到记录数
      }
    end;function TRecordStream.GetRecordSize: LongInt;
    begin
      Result:=SizeOf(TPersonRecord);//记录大小
    end;procedure TRecordStream.LastRecord;
    begin
      Seek(0,2);
      Seek(-GetRecordSize,1);//??
    end;procedure TRecordStream.NextRecord;
    begin
      if ((Position+GetRecordSize) div GetRecordSize)=GetRecordsCount then
        Raise Exception.Create('Cann''t Read Beyond end of the file')
      else
        Seek(GetRecordSize,1);
    end;procedure TRecordStream.PreviousRecord;
    begin
      if (Position-GetRecordSize)>=0 then
        Seek(-GetRecordSize,1)
      else
        Raise Exception.Create('Cann''t Read Beyond beginning of the file');
    end;function TRecordStream.ReadRecord(var _Record:TPersonRecord): LongInt;
    begin
      Result:=Read(_Record,GetRecordSize);
      Seek(-GetRecordSize,1);
    end;function TRecordStream.SeekRecord(RecordNum: Integer; Origin: Word): LongInt;
    begin
      Result:=Seek(RecordNum*GetRecordSize,Origin);//??
    end;procedure TRecordStream.SetCurRecords(RecordNum: Integer);
    begin
      if RecordNum>0 then
        Position:=(RecordNum-1)*GetRecordSize;
    end;function TRecordStream.WriteRecord(const _Record:TPersonRecord): LongInt;
    begin
      Result:=Write(_Record,GetRecordSize);
    end;end.