我有一些随机的数值,都是正整数,数值范围大概在3000到4000之间,打算用二进制的格式保存起来,请问哪儿有这方面的例子可供参考的?还请大家能提供一些源码或者在线的链接,让我学习一下,谢谢

解决方案 »

  1.   

    谢谢上面三位朋友的解答。我的这些数据是用一个stringlist保存的,现在需要把这个stringlist中的所有数据保存到一个二进制文件中。一楼说的用保存文本的方式和三楼说的直接保存,不知道怎么进行。是不是有某个语句可以直接保存成二进制文件?我现在保存成文本文件,是直接用stringlist.SaveToFile。  二楼说的FileStream,不知道能否实现我的要求,谢谢指点
      

  2.   

    StrToInt,放到一个数组里,然后保存
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
       f1:TFileStream;
          filename:String;
    begin
       OpenDialog1.Execute ;
       filename:=OpenDialog1.FileName ;
       f1:=TfileStream.Create (filename,fmopenread);
       try
          RichEdit1.Lines.LoadFromStream(f1);
          f1.Position:=0;
          Memo1.Lines.LoadFromStream(f1);
       finally
          f1.free;
       end;end;
      

  4.   


    对于通过Byte数组进行文件操作的两个函数,挺好用的,建议lz加个stringlist到TByteDynArray的转换即可。
    1. 将Byte数组生成文件procedure ByteArrayToFile(const ByteArray : TByteDynArray; const FileName : string );
    var 
     Count: integer;
     F: FIle of Byte;
     pTemp: Pointer;
    begin
     AssignFile( F, FileName );
     Rewrite(F);
     try
        Count := Length( ByteArray );
        pTemp := @ByteArray[0];
        BlockWrite(F, pTemp^, Count );
     finally
        CloseFile( F );
     end;
    end;2. 将文件生成Byte数组function FiIeToByteArray(const FileName:string ):TByteDynArray;
    const 
      BLOCK_SIZE=1024;
    var
      BytesRead,BytesToWrite,Count:integer;
      F:File of Byte;
      pTemp:Pointer;
    begin
      AssignFile( F, FileName );
      Reset(F);
      try
        Count := FileSize( F );
        SetLength(Result, Count );
        pTemp := @Result[0];
        BytesRead := BLOCK_SIZE;
        while (BytesRead = BLOCK_SIZE ) do
        begin
           BytesToWrite := Min(Count, BLOCK_SIZE);
           BlockRead(F, pTemp^, BytesToWrite , BytesRead );
           pTemp := Pointer(LongInt(pTemp) + BLOCK_SIZE);
           Count := Count-BytesRead;
        end;
      finally
         CloseFile( F );
      end;
    end;