Delphi file operation functions

Source: Internet
Author: User

A file is an ordered collection of the same type of elements and a channel for data transmission between memory and peripherals. A file is essentially a data stream. All files are actually a string of binary sequences.
File Management includes: 1. File Operations. 2. Directory operations. 3. Drive operation. Three parts.

1. Common file operation functions and procedures.
1.1 AssignFile Process
Purpose: Associate the File name of an external File with a File-type variable and initialize the variable.
Prototype: procedure AssignFile (var F; FileName: String );
F: A File type variable, which can be any data type.
FileName: name of the external file.
1.2. FileExists Function
Purpose: test whether the specified file name exists.
Prototype: function FileExists (const FileName: String): Boolean;
FileName: file name. String type.
Note: If the object exists, True is returned; otherwise, False is returned.
1.3 Append Process
Purpose: Prepare for adding text to the end of an existing file. Files are opened in write-only mode.
Prototype: procedure Append (var F: Text );
F: A TextFile type variable.
Note: This is a process of operations on text files. The variable F must be associated with an external file. If F is not associated with an external file, an exception is thrown. If F is already in the open state, the process will close F and re-open it. The file pointer position will be set at the end of the file.
1.4 Reset Process
Purpose: For text files, the Reset process will open the file in read-only mode. For a type file or a non-type file, the Reset process opens the file in read/write mode.
Prototype: procedure Reset (var F [: File; RecSize: Word]);
F: File type variable, which can be any File type.
RecSize: the file size. It is an optional option and can be specified only when F is a non-type file. If F is a non-type file, the RecSize specifies the size of records used during data transmission. If the RecSize parameter is omitted, the default record size is 128 bytes.
Note: When a file is opened during this process, an exception occurs if the file does not exist. If the file has been opened, close the file first, open it again, and put the file pointer at the beginning of the file. The default FileMode is 2. If the file mode has not changed before calling Reset, attempts to open a read-only file will fail. After the Reset process is called, if the file is empty, the Eof function returns True; otherwise, False.
1.5 Rewrite Process
Purpose: overwrite an existing file. A non-existing file will be created and opened. For an opened file, close the file and create a new file (overwrite the original file ). The file pointer is also set to the beginning of the empty file.
Prototype: procedure Rewrite (var F: File [; Recsize: Word]);
F: File type variable, which can be any File type.
RecSize: the file size. It is an optional option and can be specified only when F is a non-type file. If F is a non-type file, the RecSize specifies the size of records used during data transmission. If the RecSize parameter is omitted, the default record size is 128 bytes.
NOTE: If F is specified as an empty file name, such as AssignFile (F, ''), F is the standard output file when the Rewrite process is called. If F is a text file, F is the write-only mode. After the Rewrite process is called, Eof (F) is always True.
1.6. Flush Function
Purpose: clears the text buffer opened in the output mode to ensure that all written characters are written to the external file.
Prototype: function Flush (var t: Text): Integer;
F: F is a text variable.
Note: This function is only used for text file processing. If the operation succeeds, Flush returns 0. Otherwise, it returns the error code. When you use the Rewrite or Append process to open a text file for output. The Flush function is used to clear the File Buffer. This process ensures that all characters in the buffer zone have been written to an external file. The Flush function does not work for files opened due to input.
1.7 Read Process
Function: used to read data from a file.
Prototype: procedure Read (F, V1 [, V2,..., Vn]);
Procedure Read ([var F: Text;] V1 [, V2,..., Vn]);
F: A file variable.
V1 ...... Vn: used to store data read from files.
Note: in Delphi, the Read process has two syntaxes. If the parameter F is a type file, the Read process reads a set of data into a variable. If the parameter F is a text file variable, the Read process reads one or more values to one or more variables.
1.8 ReadLn Process
Purpose: Read a line of text from a file.
Prototype: procedure ReadLn ([var F: Text;] V1 [, V2,..., Vn]);
V: text file variable.
V1 ...... V2: used to store read text.
Note: in Delphi, The ReadLn process is used to read a line of text from a text file and jump to the next line of the text file.
1.9. Write Process
Function: used to write data to a text file.
Prototype: procedure Write ([var F: Text;] P1 [, P2,..., Pn]); // Text type file
Procedure Write (F, V1 ,... Vn); // type file
F: Text File ratio variable.
P1 ...... Pn: indicates the data written to the file.
Note: For text files, the written content should be a String type variable. For a type file, the data written must be of a specific type.
1.10 WriteLn Process
Purpose: add a line of content to a text file. That is, a line break is followed by a carriage return.
Prototype: procedure WriteLn ([var F: Text;] P1 [, P2,..., Pn]);
F: Text File ratio variable.
1.11. Eof Functions
Function: used to determine whether the file ends.
Prototype: function Eof (var F): Boolean; // used for type files and non-type files
Function Eof [(var F: Text)]: Boolean; // used for Text files
Typical application: if the file is null or the current position is after the last character, True is returned; otherwise, False is returned.
1.12. Eoln Functions
Purpose: test whether the file Pointer Points to the end of a row. It is applicable to text files. If the current position is at the end of a row or the Eof (F) function returns True, Eoln returns True; otherwise, False.
Prototype: function Eoln [(var F: Text)]: Boolean;
Typical applications: When operating a text file in a program, you sometimes need to restrict Data Reading. If you only want to read part of the data from a certain position in a row to the end of a row. The Eoln function can be used in the program for determination.
1.13 CloseFile Process
Function: Used to close files. Colleagues disconnect files on the file record disk from related file variables.
Prototype: procedure CloseFile (var F );
F: A file type variable
Note: Generally, the CloseFile process and try... Finally... Modules.
1.14. DeleteFile Function
Purpose: delete an object from the record disk.
Prototype: function DeleteFile (const FileName: string): Boolean;
Note: If the object does not exist or cannot be deleted, False is returned; otherwise, True is returned.
1.15 Erase Process
Purpose: delete an existing file.
Prototype: procedure Erase (var F );
Note: If the file is opened, close the file before deleting it.
1.16. FileGetAttr Function
Purpose: gets the attribute settings of a file.
Prototype: function FileGetAttr (const FileName: string): Integer;
NOTE: For the specific return value code, see help.

Next we will summarize the file operation procedures in Delphi. In Delphi, files are divided into three types: text files, type files, and non-type files. A text file is an ASCII file that can be read by any file editor. A type file contains a data type file defined by a programmer. A non-type file contains all other file types.

1. Text Files
Step 1 call AssignFile () to associate the file variables with one. For example:
AssignFile(MyTextFile,'TextFile.txt ');
Step 2 open the file. There are three ways to open a file: 1. the Rewrite process creates and opens a file. For an existing file, this operation will overwrite the file; 2. use the Reset process to open a file read-only; 3. append text to an existing text file using the Append process.
Step 3. Operate the file. For example, Write or Read operations.
Step 4 Use CloseFile to close an opened file.
The following is an example of text file operations.
Example 1:

Copy content to clipboard
Code:
Procedure TForm1.Button1Click (Sender: TObject );
Var
MyTextFile: TextFile; // defines the variable MyTextFile as the text file type.
Begin
(* Step 1: contact the external file with the file variable. It is equivalent to finding a "Replacement" for an external file "*)
AssignFile (MyTextFile, 'TextFile.txt ');
(* Determines whether the operated file exists. If it does not exist, the file is created. *)
If FileExists('TextFile.txt ') then
(* Step 2: Open the file. Before opening the file, it is best to determine whether the file exists. *)
Append (MyTextFile)
Else
Rewrite (MyTextFile );
Try
(* Step 3: Operate the file. *)
WrtieLn (MyTextFile, 'text added at the end of the file! ');
Flush (MyTextFile); // clears the buffer to ensure that the string has been written to the file.
Finally
(* Step 4: Close the opened file. CloseFile is often used with try... Finally combination, used to recycle Resources *)
CloseFile (MyTextFile );
End;
End;

2. type file
Step 1 define the data structure
Step 2 call AssignFile () to associate the file variables with a type file.
Step 3 open a type file.
Step 4: operate on type files.
Step 5 close an opened file.
The following example demonstrates how to operate a type file.
Example 2:

Copy content to clipboard
Code:
(* Step 1: define the data structure. *)
Type
TPerson = record
Name: string [20];
Sex: string [4]
Age: Integer;
End;

Procedure TForm1.Button1Click (Sender: TObject );
Var
PersonFile: file of TPerson; // declare the variable "PersonFile" as a TPerson file.
Person: TPerson;
Begin
(* Step 2: Associate the file. *)
AssignFile (PersonFile, 'person. dat ');
(* Step 3: open the file. *)
Reset (PersonFile );
Try
While not Eof (PersonFile) do
(* Step 4: Operate the file. *)
Begin
Read (PersonFile, Person );
Memo1.Lines. Add (Person. Name );
Memo1.Lines. Add (Person. Sex );
Memo1.Lines. Add (IntToStr (Person. Age ));
End;
Finally
(* Step 5: close the file. *)
CloseFile (PersonFile );
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.