//dbf文件头结构
//这是C定义
struct dbf_head { /* DBF文件头结构 */
  char vers; /* 版本标志*/
  unsigned char yy,mm,dd; /* 最后更新年、月、日 */
  unsigned long no_recs; /* 文件包含的总记录数 */
  unsigned short head_len,rec_len; /* 文件头长度,记录长度 */
  char reserved[20]; /* 保留 */
 };//这是我在Delphi中定义的
type
  Tdbf_head = record
    vers : char ; //版本标志
    yy,mm,dd :char;  // 最后更新年、月、日
    no_recs : longword ; // 文件包含的总记录数;
    head_len,rec_len : word;  //文件头长度,记录长度
    reserved : array[0..19] of char ;
end;问题1 :
  不知转换成Delphi定义的正不正确?问题2:
  读DBF文件头信息;
var
  f : File;
  p : ^Tdbf_head;
  no_Recs,head_len,rec_len,Rec_Count : Integer;
begin
  AssignFile(f,'e.dbf');
  Reset(f);
  try
    p := AllocMem(sizeof(Tdbf_head));
    BlockRead(f,P^,Sizeof(file_head));
    //记录总数 获得值为6208
    no_Recs := P^.no_recs;
    //头文件长度;获得值为1032
    head_len := P^.head_len ;
    //记录长度;获得值为137
    rec_len := P^.rec_len;
    //计算字段总数;获得值为23
    Rec_Count :=  (head_len -296) div 32 ;    Edit1.Text := IntToStr(no_Recs) +' ' + IntToStr(head_len) +' '+ IntToStr(rec_len)  +' '+IntToStr(Rec_Count) ; //这句出错了,请问是什么原因呢?
  finally
    FreeMem(P);
    CloseFile(f);
  end;

解决方案 »

  1.   

    //这是我在Delphi中定义的
    type
      Tdbf_head = packed record
        vers : char ; //版本标志
        yy,mm,dd :char;  // 最后更新年、月、日
        no_recs : longword ; // 文件包含的总记录数;
        head_len,rec_len : word;  //文件头长度,记录长度
        reserved : array[0..19] of char ;
    end;
      

  2.   

    head_len,rec_len : word;  //文件头长度,记录长度
    也不对
      

  3.   

    按楼主的程序看,偶只发现是在BlockRead报错,须将Reset(f)改为Reset(f,1)
      

  4.   

    另外年月日的类型在Delphi当中使用Byte比较,即通常以C当中的unsigned char就可以直接在Delphi当中写Byte.这主要是考虑到char是有符号的。
      

  5.   

    TO: unsigned(僵哥(当程序语言成为普及的第三语言之后……)) 
        fontain() 谢谢!谢谢高人指点。
      

  6.   

    var
      f : File;
      p : ^Tdbf_head;
      no_Recs,head_len,rec_len,Rec_Count : Integer;
      buf : PChar ;
      Read_COunt : Integer;
      i : Integer;
    begin
      AssignFile(f,'e.dbf');
      Reset(f,1);
      try
        p := AllocMem(sizeof(Tdbf_head));
        BlockRead(f,P^,Sizeof(file_head));
        //记录总数
        no_Recs := P^.no_recs;
        //头文件长度;
        head_len := P^.head_len ;
        //记录长度;
        rec_len := P^.rec_len;
        //计算字段总数;
        Rec_Count :=  (head_len -296) div 32 ;
      //移dbf数据开始位置 
        Seek(f,P^.head_len+1);
      //分配一条记录长度的内存; 
        buf := AllocMem(rec_len);
      //从文件中读取一条记录的内容放入buf中; 
        BlockRead(f,buf,rec_len,read_count); //这条语句读 出错,
      ...
      finally
        FreeMem(P);
        FreeMem(buf);
        CloseFile(f);
      end;--
      注:DBF文件中的数据全是以ASCIC码保存的请问我在什么地方写错了????