Delphi file Operations-read files-write files-manipulate files

Source: Internet
Author: User

Original from: 100 Script web

A description of several variables in this example
Tfilerec = record//records definition
Day:integer;
...//Other definitions
End
F:file of Tfilerec; Standard input/output files
Filrec:tfilerec; Record Data
FileName; Name of the record file
About actions related to recording files
(1) Opening of the recording file
(2) Read-in and display of recorded files
(3) Increase in records
(4) Deletion of records
(5) Modification of records
(6) Deletion of record files

1, the opening of the record file
The method to use:
Assingfile (F,filename):
Reset (f)://Open a record file that already exists
Rewrite (f); Create a new record file
FileSize (f); Returns the number of record file records


Procedure Openrecfile (const filename:string; var f:tfilerec);
Begin
Assingfile (F, FileName):
Try
Reset (f);
except//Open file exception handling
On Einouterror do
Begin
Try
If FileExists (FileName) = False then//If the file does not exist, create
ReWrite (Methodfile);
else//file exists but does not open, indicating a problem with this file
Messagedlg (' file cannot be opened ', mtwarning, [Mbok], 0);
except//When creating a new file, exception handling occurs
On Einouterror do
Messagedlg (' file cannot be created ', mtwarning, [Mbok], 0);
End
End

End
End

2. log file read-in
The method that needs to be used

Read (f, Filerec);


Procedure Readdata_fromrecfile;
Begin
For I: = 1 to FileSize (f) do
Begin
Read (f, Filerec);
...//other processing (e.g. display of records, etc.)
End
End

3, the increase of the record
needs to use the method  
Seek (F, RecNo);  //RECNO represents the record number of the location to be set
Write (f, Filerec)
Basic idea:
Get the location of the specified record and move the records after that location one by one

(1) Add a record at the end of the file
Procedure insertdata_atrecfile_last (CONST DATA:TFILEREC);  Data to be inserted
begin
Seek (f,  FileSize (f));
Write (f, Data);
End;   
(2) Add a record before the file's specified location
Procedure insertdata_atrecfile (const index:  integer);   Index number
const data:tfilerec; The data to be inserted
var
ni:integer;
  Begin
if  (Index > FileSize (f))  or  (Index < 0)  then
Exit;
    For ni: = FileSize (f)-1 downto index do
Begin
Seek (f, NI);
Read (f, Filerec);
Seek (F, NI + 1);
Write (f, Filerec);
End;
Seek (f, Index);
Write (f, Data);
End;   
4, deletion of the record
The method to be used  
Seek (F, Index);   Index represents the record number of the location to be fixed
Write (f, Filerec)
Truncate (f); Delete the index file after the specified index number
Basic idea:
Gets the specified location and moves the records after that position one by one forward. The file was truncated before the last record.

(1) Delete the record of the specified index number
Procedure Deletedata_fromrecfile (const index:integer);
Var
Ni:integer;
Begin
if (Index < 0) or (FileSize (f) = 0) Then
Exit;
For NI: = Index + 1 to FileSize (f)-1 do
Begin
Seek (f, NI);
Read (f, Filerec);
Seek (f, nI-1);
Write (f, Filerec);
End
Seek (F, FileSize (f)-1); Truncate last record
Truncate (f);
End
5. Modification of records
Procedure Modifydata_atrecfile (const index:integer; const DATA:TFILEREC);
Begin
if (Index < 0) or (FileSize (f) = 0) or (Index > FileSize (f)-1) Then
Exit;
Seek (f, Index);
Write (f, Data);
End
6, the closing of the record file
CloseFile (f);

Delphi file Operations-read files-write files-manipulate files

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.