How does Delphi handle different types of files

In program design, we often encounter situations where we need to deal with files, directories and drives. Here we will describe how to deal with different types of files.
Mainly includes how to use the TFileStream class to encapsulate the input/output of files and how to use the main feature of 32-bit windows-memory mapped files. You will learn how to create a TMemoryMappedFile class that encapsulates the memory-mapped file function and use it to perform text queries in text files. It also provides some methods for selecting a drive, querying files in a tree directory, and obtaining file version information. Make you have a deep understanding of files, directories and drives.
Process file input/output
You will process three types of files: text files, type files, and binary files
The following sections are about file input/output. Text files are ASCII texts that can be read by any text editor. Type files refer to files containing data types defined by the programmer. Binary files include all other types. It is a general term for files containing arbitrary format or unformatted data.
Processing of text files
This article describes how to process text files using procedures and functions built into the Object Pascal runtime library. Before any processing on the text file, the text file must be opened first. Declare variable: A text file can be referenced through this variable.
Opening a file requires two steps: The first is AssignFile(), which can associate file variables with a file. Then proceed to the second step: open the file. There are three ways to open a file: use the ReWrite() process to create and open the file. For an existing file, this operation will cause the file to be overwritten; use the Reset() process to open a file read-only; use the Append() process to Append text to existing files.
Note that Reset() will open type files and untyped files in read-write mode. You can use CloseFile() to close an open file
File opening and closing
AssignFile | Associate an external file name with a file variable |
Reset | Open an existing file, text file (read only), other files (read and write) |
ReWrite | Create and open a new file (or overwrite an existing file) |
Append | Open a file in append mode (only applicable to text files) |
CloseFile | Close an open file |
FileOpen | Open a specific file and return the file handle |
FileCreate | Create a file with a given file name and return the file handle |
FileClose | Close a file with a specific handle |
The following three files are mainly for internal use of the system, and are often used in the programming process of file copying. The objects they operate on are file handles, not file variables.
File location
Seek | Move the current position of the file to the specified part |
FilePos | Returns the current position of the file |
Eoln | Return line end flag |
EOF | Return end of file mark |
FileSeek | Change the position of the current file pointer |
There is another question, how to judge the file has been opened? Should I close the file after reading and writing, and open it again when I read or write next time?
Use TFileStream
For file operations through Byte arrays, they are often used in FTP. I also found these two functions when Delphi called Web Service to upload and download files. They are very easy to use and I recommend them to everyone. (Statement: Not written by me)
<1>Generate Byte array into file
procedure
ByteArrayToFile(
Const
ByteArray : TByteDynArray;
Const
FileName :
String
);
var
Conut :
Integer
;
F :
File
of
Byte
;
pTemp :
Pointer
;
begin
AssignFile(F, FileName);
ReWrite(F);
try
Count := Length(ByteArray);
pTemp := @ByteArray[
0
];
BlockWrite(F, pTemp^, Count);
finally
CloseFile(F);
end
;
end
;
function
FileToByteArray(
const
FileName :
String
) :TButeBtnArray;
const
BLOCK_SIZE =
1024
;
var
BytesRead, BytesToWrite, Count :
Integer
;
F :Filw
of
Byte
;
pTemp :
Pointer
;
begin
AssignFile(F, FileName);
Reset(F);
try
Count := FileSize(F);
SetLength(Result, Count);
pTemp := @Result[
0
];
BytesRead := BLOCK_SIZE;
while
(BytesRead = BLOCK_SIZE)
do
begin
BytesToWrite := Min(Count, BLOCK_SIZE);
BlockRead(F, pTemp^, BytesToWrite, BytesRead);
pTemp :=
Pointer
(
LongInt
(pTemp)+BLOCK_SIZE);
Count := Count-BytesRead;
end
;
finally
CloseFile(F);
end
;
end
;
function
Encrypt(mStr :
String
; mKey :
String
) :
String
;
var
I, J :
Integer
;
begin
J:=
1
;
Result :=
''
;
for
I:=
1
to
Length(mStr)
do
begin
Result := Result +
Char
(Ord(mStr[I]
xor
Ord(mKey[j]));
if
J +
1
<= Length(mKey)
then
Inc(J)
else
J:=
1
;
end
;
{Add steps yourself}
end
;
function
Decrypt(mStr :
String
; mKey :
String
) :
String
;
var
I,J:
Integer
;
begin
J :=
1
;
Result :=
''
;
{Add steps yourself}
for
I :=
1
to
Length(mStr)
do
begin
Result := Result +
Char
(Ord(mStr[I]
xor
Ord(mKey[J]));
if
J+
1
<= Length(mKey)
then
;
Inc(J)
else
J:=
1
;
end
;
end
;
procedure
TForm1
.
Button1Click(Sender : TObject);
const
cKey1 =
'Who wants to try how to break'
;
cKey2 =
'I don’t want to play this way (1) I don’t want to play this way (2) I don’t want to play this way (3)'
;
cKey3 =
'Memo2.Text := Decrypt(Encrypt(Memo1.Text, cKey), cKey);'
;
var
S :
String
;
//Encrypted characters
begin
S := Encrypt(Encrypt(Encrypt(Memo1
.
Text, cKey1), cKey2), cKey3);
ShowMessage(S);
Memo2
.
Text := Decrypt(Decrypt(Decrypt(S, cKey3), cKey2), cKey1);
end
;