Two important functions for operating binary files in Delphi

Source: Internet
Author: User

File operations through byte arrays are often used in FTP. I also found these two functions when Delphi calls web service to upload and download files, which is quite useful, we recommend it to you. (Statement: not written by myself)

1. Generate a byte array File ProcedureBytearraytofile ( ConstBytearray: tbytedynarray; ConstFilename: 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. Generate a byte array from the file FunctionFiietobytearray ( ConstFilename: 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.