解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    id:string[5];
    stid:string[8];
    lon,lat,temp,rain,tim:single;
    nlev,flag,ln:integer;
    f:textfile;
    g:file;
    stemp:string;
    begin
      tim:=0.0;
      nlev:=1;
      flag:=1;
      assignfile(f,'12082805.000');
      reset(f);
      readln(f, stemp);
      readln(f,stemp);
      readln(f, stemp);
      readln(f,stemp);
      readln(f, stemp);
      readln(f,stemp);
      readln(f, stemp);
      readln(f,stemp);
      readln(f, stemp);
      readln(f,stemp);
      readln(f, stemp);
      readln(f,stemp);
      readln(f, stemp);
      readln(f, stemp);
      assignfile(g,'12082805.grd');
      rewrite(g,2);
      while not eof(f) do
      begin
        readln(f,id,lon,lat,temp,rain);
        stid:=id;
        blockwrite(g,stid,1);    blockwrite(g,lat,4);
        blockwrite(g,lon,4);
        blockwrite(g,tim,4);
        blockwrite(g,nlev,2);
        blockwrite(g,flag,2);
        blockwrite(g,rain,4);
      end;
      nlev:=0;
      blockwrite(g,stid,2);
      blockwrite(g,lat,4);
      blockwrite(g,lon,4);
      blockwrite(g,tim,4);
      blockwrite(g,nlev,2);
      blockwrite(g,flag,2);end;end.
    我改成这样,生成的文件大小和fortran的差很多,fortran的37K,delphi的只有17k,该怎么修改?
      

  2.   

    这是12082808.000
    diamond 3 12年08月28日05时24小时降水量
    12 08 28 05   -2
          8
          1      5     10     25     50    100    150    200
          1     25     39
      122.7   37.3  122.5   36.7  120.8   36.2  119.3   35.0  120.4   34.3
      121.1   32.7  122.3   30.9  122.5   30.1  121.5   28.0  119.8   25.0
      116.1   22.6  111.0   21.0  111.0   18.6  109.6   18.1  108.3   18.5
      108.9   19.9  109.3   21.0  106.4   21.6  104.1   22.2  101.6   20.8
       98.7   22.0   97.1   24.1   97.7   27.0   91.7   26.6   85.7   27.7
       78.9   30.6   73.3   37.1   74.1   40.7   87.6   49.8   98.5   43.9
      110.4   44.3  122.0   54.7  135.7   48.3  131.5   42.2  125.2   39.4
      121.3   38.5  121.2   40.3  118.5   38.6  120.8   38.1
        1    398
    58608  115.55   28.06   30    .01
    55472   88.63   30.95 4671     .5
    55696   92.47   28.42 3861    .01
    56317   94.21   29.21 2952     .3
    55597   91.68   29.03 3741    .01
    54254  124.05   42.53   99     .9
    52868  101.43   36.03 2238    4.5
    52955  100.75   35.58 3202    2.7
    56034   97.13   33.80 4418     .6
    53588  113.53   39.03 2898   18.5
    55228   80.08   32.50 4279     .5
    56228   96.91   30.05 3261    2.2
    53578  112.43   39.30 1092    2.2
    55690   91.95   27.98 4281     .9
    52889  103.88   36.05 1518     .1
    54266  125.63   42.53  341    .01
    54326  119.70   41.38  423     .3
    55294   91.10   32.35 4801    3.8
    55279   90.02   31.37 4701    2.2
    50953  126.77   45.75  143   20.9
    53490  114.05   40.43 1027   18.9
    56342   98.60   29.68 3871   12.0
    56223   95.83   30.75 3640     .8
    54335  122.01   41.68   38    4.2
    54471  122.20   40.67    4    .01
    56109   93.78   31.48 3941    8.7
    56116   95.60   31.42 3874    8.0
    56331   97.83   29.66 3781    3.5
    56106   93.78   31.88 4024    5.3
    55598   91.76   29.25 3553    4.7
    56128   96.60   31.21 3811     .9
    56548   99.28   27.16 2322    3.8
    56586  103.75   27.33 1950    3.2
    53663  111.81   38.93 1402     .6
    53673  112.70   38.75  838    7.3
    53664  111.13   38.46 1014    2.6
    56172  102.23   31.90 2666    3.5
    57399  114.97   31.18   59    5.7
    54094  129.60   44.57  242    1.1
    54523  117.10   39.40    7    .01
    53923  107.63   35.73 1423    1.7
    56093  104.01   34.43 2316    1.2
    52984  103.18   35.58 1918    3.6
    56137   97.17   31.15 3307   15.5
    56202   93.28   30.66 4490    2.2
    52754  100.13   37.33 3301    1.1
    56046   99.65   33.75 3968     .6
    55248   84.41   32.15 4416    4.9
    52996  105.00   35.38 2450    6.8
      

  3.   

    这样应该可以的:{$APPTYPE CONSOLE}
    program test;
    uses SysUtils;  // System.SysUtils;type
      TOutputData = packed record
        stid: array[0..7] of AnsiChar;
        lat, lon: single;
        tim: single;
        nlev,flag: integer;
        rain: single;
      end;const
      BypassedLines = 14;
      VerboseList = true;  SourceFile = '12082808.000';
      DestFile = '12082805.grd';  MSG_FileNotFound = 'File not found: "%s".';
      MSG_CreateFileFailed = 'Could not create file: "%s".';
      MSG_ReadFileFault = 'Read file fault: "%s".';
      MSG_WriteFileFault = 'Read file fault: "%s".';
      MSG_ConvertingProgress = 'Converting "%s" to "%s" ... ';
      MSG_DataListFormat = '%8s %10.2f %10.2f %10.2f %10d %10d %10.2f';
      MSG_ConvertFileComplete = '%d bytes, done.';procedure Abort(Message: string);
    begin
      writeln(Message);
      Halt(1);
    end;procedure ConvertFileFormat(SourceFile, DestFile: string);
    var
      f: text;
      g: file;
      o: TOutputData;
      i: integer;
      dumb: single;
    begin
      AssignFile(f, SourceFile);
      {$I-}
      Reset(f);
      {$I+}
      if IOResult <> 0 then
        Abort(Format(MSG_FileNotFound, [SourceFile]));  AssignFile(g, DestFile);
      {$I-}
      Rewrite(g, 1);
      {$I+}
      if IOResult <> 0 then
      begin
        CloseFile(f);
        Abort(Format(MSG_CreateFileFailed, [DestFile]));
      end;
      
      o.tim := 0.0;
      o.nlev := 1;
      o.flag := 1;  write(Format(MSG_ConvertingProgress, [SourceFile, DestFile]));  for i := 1 to BypassedLines do
      begin
        {$I-}
        readln(f);
        {$I+}
        if IOResult <> 0 then
        begin
          CloseFile(f);
          CloseFile(g);
          Abort(Format(MSG_ReadFileFault, [SourceFile]));
        end;
      end;  while not EOF(f) do
        begin
          {$I-}
          readln(f, o.stid, o.lon, o.lat, dumb, o.rain);  // dumb for bypassing one column
          {$I+}
          if IOResult <> 0 then
          begin
            CloseFile(f);
            CloseFile(g);
            Abort(Format(MSG_ReadFileFault, [SourceFile]));
          end;      if VerboseList then
          begin
            writeln;
            writeln;
            writeln(Format(MSG_DataListFormat, [o.stid, o.lat, o.lon, o.tim, o.nlev, o.flag, o.rain]));
            writeln;
          end;      {$I-}
          BlockWrite(g, o, sizeof(o));
          {$I+}
          if IOResult <> 0 then
          begin
            CloseFile(f);
            CloseFile(g);
            Abort(Format(MSG_WriteFileFault, [DestFile]));
          end;
        end;  writeln(Format(MSG_ConvertFileComplete, [FileSize(g)]));
      CloseFile(f);
      CloseFile(g);
    end;{ main }
    begin
      ConvertFileFormat(SourceFile, DestFile);
    end.
      

  4.   

    啊,这个 MSG_WriteFileFault = 'Read file fault: "%s".';
    应该是:MSG_WriteFileFault = 'Write file fault: "%s".';
    复制/粘贴之后没改。
      

  5.   

    还有这个:
    nlev=0
    write(2)stid,lat,lon,tim,nlev,flag
    没写,少写28个字节,你自己补上吧。
      

  6.   

    还有一个问题,Fortrnan程序中:
    read(1,*)
    read(1,*)
    read(1,*)
    这应该是忽略了文件开头的3行,而不是14行,我是受了你的程序的影响,所以BypassedLines = 14;这行应该改成BypassedLines = 3;
      

  7.   

    那没关系的,文件的格式有了变化,数据都是用不上的。谢谢了,程序没问题,结果和Fortran的一样了。谢谢了。有一点不明白,麻烦你在讲一下,你在记录里定义stid: array[0..7] of AnsiChar;,数据中第一个数据为5列,read的时候怎么只读5列?我定义stid:sting[8]时,read的时候就把第二个数据的第一列读了进去。数据58608  115.55   28.06   30    .01中第一列和第二列之间是有两个空格的。
      

  8.   

    还有个奇怪的问题,我比较了一下fortan和delphi生成的二进制文件,两个文件的第八个字节是不同的,这是怎么回事?
      

  9.   

    string[8]这么定义它非读满8个字符,字符数组的话读到分隔的空格、TAB之类的字符就结束。
    另外,Delphi读过来的stid末尾附加有一个结束符号0,这个应该不影响使用,显示输出都没有问题。