File Operations in Delphi are like Pascal's syntax. The first programming language used to start programming is Pascal. Unfortunately, file operations can only be completed after reading a book. Ah .... If you think of a solution, it is better to encapsulate operations on files into classes for ease of use. Second, when writing code for operations on files, it can be used as an example.
An incomplete tsaftextfile class implements the basic read/write function for textfile. You are welcome to improve and supplement it.
//************************************** ******************************
//
// Name: sinoprise function library for Delphi
//
// Author: Shuguang Yin
// Sinoprise Technology Lab
// Create: 2005-11-15
//
// Official Website: http://www.sinoprise.com
// Sinoprise technology community: http://www.usenix.cn
//
//************************************** ******************************
Unit saf_textunit;
Interface
Uses
Windows, sysutils;
Const
Saf_open_file_result_ OK = 0; // 0 is normal
Saf_open_file_result_file_exist =-1; // The-1 file already exists.
Saf_open_file_result_file_not_exist =-2; // The-2 file does not exist.
Saf_open_file_result_create_file_error =-3; //-3: An error occurred while creating the file.
Saf_open_file_result_open_file_error =-4; //-4 failed to open the file
Saf_open_file_result_file_already_open =-5; // The-5 file has been opened.
Saf_open_file_result_unknown_error = 1; // unknown error
Type
Tsafopenfileopt = (
Soffilecreate, // create a file. An error occurs when the file exists.
Soffileopenreadonly, // open in read-only mode. An error occurs when the file does not exist.
Soffileopenreadwrite, // open the file. An error occurs when the file does not exist.
Soffileopencreate, // when the file exists, open the file. When the file does not exist, create the file
Soffileappendonly, // open the file and prepare to add data. If the file does not exist, an error is returned.
Soffileappendcreate, // when the file exists, open the file in append mode. When the file does not exist, create the file
Soffilerewritecreate // when the file exists, overwrite the file. When the file does not exist, create the file
);
Esaftextfileexcetpion = Class (exception)
Private
Ferrorcode: integer;
Fdescribe: string;
Ffilename: string;
Fopenfileoption: tsafopenfileopt;
Public
Property openfileoption: tsafopenfileopt read fopenfileoption;
Property filename: String read ffilename;
Property errorcode: integer read ferrorcode;
Property describe: String read fdescribe;
Constructor create (FN: string; Ofo: tsafopenfileopt; EC: integer; des: string );
End;
Tsaftextfile = Class
Private
Ftextfile: textfile;
Ffilename: string;
Fopenfileoption: tsafopenfileopt;
Ffileopen: Boolean;
Function getseekeof: Boolean;
Function getseekeol: Boolean;
Public
Function Readline: string;
Procedure writeline (value: string );
Function read: string;
Procedure write (value: string );
// Return value:
//> 0: the number of rows correctly moved
// = 0 the current pointer of the file is not moved
Function movenextlines (nmvlines: integer = 1): integer;
// Whether the file ends
Property EOF: Boolean read getseekeof;
// Whether the row ends
Property EOL: Boolean read getseekeol;
// Parameters used to open a file
Property openfileoption: tsafopenfileopt read fopenfileoption;
// File name
Property filename: String read ffilename;
// This function creates a file when the file exists and the file does not exist.
Constructor create (afilename: string; openopt: tsafopenfileopt );
Destructor destroy; override;
End;
Implementation
{Tsaftextfile}
Constructor tsaftextfile. Create (afilename: string; openopt: tsafopenfileopt );
VaR
Fexist: Boolean;
Begin
Assignfile (ftextfile, afilename );
Ffilename: = afilename;
Fopenfileoption: = openopt;
Ffileopen: = false;
Fexist: = fileexists (afilename );
Case openopt
// Create a file. An error occurs when the file exists.
Soffilecreate: Begin
If fexist then begin
Raise esaftextfileexcetpion. Create (ffilename, fopenfileoption,
Saf_open_file_result_file_exist, 'file already exist ');
End;
Rewrite (ftextfile );
End;
// Open the file in read-only mode. An error occurs when the file does not exist.
Soffileopenreadonly: Begin
If not fexist then begin
Raise esaftextfileexcetpion. Create (ffilename, fopenfileoption,
Saf_open_file_result_file_not_exist, 'file does not exist ');
End;
Reset (ftextfile );
End;
// Open the file. An error occurs when the file does not exist.
Soffileopenreadwrite: Begin
If not fexist then begin
Raise esaftextfileexcetpion. Create (ffilename, fopenfileoption,
Saf_open_file_result_file_not_exist, 'file does not exist ');
End;
Rewrite (ftextfile );
End;
// Open the file when the file exists. Create the file when the file does not exist.
Soffileopencreate: Begin
Rewrite (ftextfile );
End;
// Open the file and add data. If the file does not exist, an error is returned.
Soffileappendonly: Begin
If not fexist then begin
Raise esaftextfileexcetpion. Create (ffilename, fopenfileoption,
Saf_open_file_result_file_not_exist, 'file does not exist ');
End;
Append (ftextfile );
End;
// When the object exists, open the object in append mode. When the object does not exist, create the object.
Soffileappendcreate: Begin
If fexist then
Append (ftextfile)
Else
Rewrite (ftextfile );
End;
// If the object exists, overwrite the object. If the object does not exist, create the object.
Soffilerewritecreate: Begin
Rewrite (ftextfile );
End;
End;
Ffileopen: = true;
End;
Destructor tsaftextfile. Destroy;
Begin
If ffileopen then closefile (ftextfile );
Inherited;
End;
Function tsaftextfile. getseekeof: Boolean;
Begin
Result: = seekeof (ftextfile );
End;
Function tsaftextfile. getseekeol: Boolean;
Begin
Result: = seekeoln (ftextfile );
End;
Function tsaftextfile. Read: string;
Begin
System. Read (ftextfile, result );
End;
Function tsaftextfile. Readline: string;
Begin
System. readln (ftextfile, result );
End;
Function tsaftextfile. movenextlines (nmvlines: integer): integer;
VaR
I: integer;
Begin
Result: = 0;
For I: = 1 to nmvlines do begin
If self. EOF then exit;
Readln (ftextfile );
INC (result );
End;
End;
Procedure tsaftextfile. Write (value: string );
Begin
System. Write (ftextfile, value );
End;
Procedure tsaftextfile. writeline (value: string );
Begin
System. writeln (ftextfile, value );
End;
{Esaftextfileexcetpion}
Constructor esaftextfileexcetpion. Create (FN: string; Ofo: tsafopenfileopt;
EC: integer; des: string );
Begin
Ferrorcode: = EC;
Fdescribe: = des;
Ffilename: = FN;
Fopenfileoption: = Ofo;
Inherited create (fdescribe );
End;
End.
Simple Example:
Procedure .......
VaR
T: tsaftextfile;
Begin
Try
T: = tsaftextfile. Create ('C:/saftextfile.txt ', soffileopencreate );
Try
T. dosomething ()
Finally
T. Free;
End;
Except
On E: esaftextfileexception do begin
Showmessage (inttostr (E. errorcode) + ':' + E. Describe );
End;
End
End;