Delphi中的操作二进制文件的两个重要函数

转自:https://blog.csdn.net/bwqyujianweilai2/article/details/49362953

未验证

Delphi中的操作二进制文件的两个重要函数

对于通过Byte数组进行文件操作的,在FTP中经常会使用到,我也是在Delphi调用Web Service进行文件的上传和下载时找到这两个函数的,挺好用的,推荐给大家。(申明:非本人所写)

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;
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值