Non-type file read/write

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; button1: tbutton; handler: tbutton; button3: tbutton; button4: tbutton; button5: tbutton; button6: tbutton; button7: handler; Procedure destroy (Sender: tobject ); procedure button3click (Sender: TOB Ject); Procedure button4click (Sender: tobject); Procedure button5click (Sender: tobject); Procedure button6click (Sender: tobject); Procedure button7click (Sender: tobject ); private {private Declarations} public {public declarations} end; var form1: tform1; implementation {$ R *. DFM} const filename = 'C: \ temp \ binary. dat '; var F: file; // write a byte procedure tform1.button1click (Sender: tobject) in a non-typed manner; var B: Byte; begin assignfile (F, filename); rewrite (F, 1); // The second parameter indicates that 1 byte is treated as a write unit B: = 65; blockwrite (F, B, 1); // write; parameter 2 indicates the content to be written; parameter 3 indicates the write once. closefile (f); // implements binary. dat size: 1 byte; content: A (available in Notepad) end; // read a byte in non-typed mode procedure tform1.button2click (Sender: tobject); var B: byte; begin assignfile (F, filename); reset (F, 1); // treats one byte as a read unit blockread (F, B, 1 ); // read once; put B showmessage (CHR (B); // A closefile (f); End; // Write more bytes procedure tform1.button3click (Sender: tobject); var Buffer: array [0 .. 127] of byte; I: integer; // stores multiple bytes. you need to use an array of begin for I: = low (buffer) to high (buffer) Do buffer [I]: = I; // assign a value to the array. Note that the current array size is 128 assignfile (F, filename); rewrite (F, 32 ); // The 32 bytes are regarded as a read unit. Note that the number and the buffer size must be multiples of blockwrite (F, buffer, 4). // how many times does it need to be written? 128/32 = 4 closefile (f); // the size of the written file must be 128 bytes, but it may not be visible in notepad, because the binary is not the end of the text; // read procedure tform1.button4click (Sender: tobject); var Buffer: array [0 .. 127] of byte; I: integer; begin assignfile (F, filename); reset (F, 4); // 4 bytes are treated as a read unit; for that multiple relationship, here is generally 1 blockread (F, buffer, 32); // Of course, it takes 32 times to read closefile (f); // how can I display it? Or use memo to display characters? Number? Memo1.clear; for I: = low (buffer) to high (buffer) Do begin memo1.lines. add (inttohex (buffer [I], 1); end; (* display result: 0 1 2 3... 7D 7E 7f *) end; // read/write char and actual read/write bytes procedure tform1.button5click (Sender: tobject); var carr1, carr2: array [0 .. 4] of char; I: integer; num: integer; // records the actual number of bytes read and write. Begin for I: = low (carr1) to high (carr1) do carr1 [I]: = CHR (65 + I); // fill in a B c d e assignfile (F, filename); rewrite (F, 1 ); // create blockwrite (F, carr1, length (carr1), num); // parameter 4: num is the actual number of bytes written showmessage (inttostr (Num )); // 5 reset (F, 1); // re-open blockread (F, carr2, length (carr2), num); // parameter 5: num is the actual number of bytes read showmessage (inttostr (Num); // 5 showmessage (carr2); // ABCDE closefile (f); end; // write a long string, read its hexadecimal code procedure tform1.button6click (Sender: tobject); var P: pchar; B: byte; begin P: = 'in case of Delphi blog'; assignfile (F, filename); // associated file name rewrite (F, 1); // create and open blockwrite (F, P, length (p); // write filemode: = fmopenread; // you can set the file to read-only reset (F, 1); // open memo1.clear for reading; while not EOF (f) Do begin blockread (F, B, 1 ); memo1.text: = memo1.text + inttohex (B, 2) + ''; end; // display the result: 70 F0 45 00 08 F7 12 00 A6 FB 43 00 A0 1A E5 00 FF C5 closefile (f); end; // copy the procedure tform1.button7click (Sender: tobject) file ); vaR fromf, TOF: file; numread, numwritten: integer; Buffer: array [1 .. 2048] of byte; begin assignfile (fromf, 'c: \ temp \ test1.dat '); // assume that this file is reset (fromf, 1); assignfile (TOF, 'c: \ temp \ test2.dat '); rewrite (TOF, 1); repeat blockread (fromf, buffer, sizeof (buffer), numread); blockwrite (TOF, buffer, numread, numwritten ); until (numread = 0) or (numwritten numread); {numread = 0 indicates that the read is finished; numwritten numread indicates that the disk space is insufficient} closefile (fromf); closefile (TOF); 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.