Text File read/write

Source: Internet
Author: User
Preface:
Delphi supports three types of files: text files, record files, and non-type files.

Text files are read and written in the unit of behavior. Because the length of each row is not necessarily the same, the exact position of the given row in the file cannot be calculated, so it can only be read and written in sequence.

A text file can only be opened for reading or writing. Simultaneous read and write operations on an opened text file are not allowed.

To open a text file, two steps are required: 1. The file variables are associated with the file name; 2. Initialization and read/write.

1. Association of file variables with file names:
Assignfile (vartxt, filename );

If filename is omitted, the current directory is used by default.

2. There are three methods to initialize read/write:
(1) Reset: Read-only open, pointer moved to the file header;

(2) rewrite: Create and open a new file. Write only;

(3) append: append the pointer from the end of the file.

When a file does not exist, using reset or append will cause an I/O exception.

Close the file with closefile.

To maintain compatibility, Delphi also allows you to use assign to establish associations and close files.

// Read/write example: Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; button5: tbutton; button6: tbutton; button7: tbutton; Procedure encode (Sender: tobject ); procedure extract (Sender: tobject); Procedure button3click (Sender: tobject); Procedure extract (Sender: tobject ); procedure button7click (Sender: tobject); Private {private Declarations} public {public declarations} end; var form1: tform1; implementation {$ R *. DFM} var F: text; // The same filename: String = 'C: \ temp \ test.txt 'As textfile and text; // write the file procedure tform1.button1click (Sender: tobject ); begin assignfile (F, filename); rewrite (f); // overwrite the existing file writeln (F, 'First row'); writeln (F, 'row 2 '); closefile (f); end; // read the file (write the file first) Procedure tform1.button2click (Sender: tobject); var S: string; begin assignfile (F, filename); reset (f); // read-only open readln (F, S); // read showmessage (s); // display: first row readln (F, S); // continue reading showmessage (s); // display: Second row closefile (f); end; // append procedure tform1.button3click (Sender: tobject); begin assignfile (F, filename); append (f); // open and prepare to append writeln (F, '3rd row'); writeln (F, 'row 4'); closefile (f); end; // read all (display with Memo help) Procedure tform1.button4click (Sender: tobject); var S: string; begin assignfile (F, filename); reset (f); // read-only open memo1.clear; while not EOF (f) Do begin readln (F, S); memo1.lines. add (s); end; closefile (f); end; // write data in procedure tform1.button5click (Sender: tobject); // the function of this procedural function is: use spaces to generate n length functions addspace (S: string; N: Word): string; begin while length (s)

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.