Three file types used for reading and writing (I/O) in Delphi
1. Old pascal file type
File types represented by old file variables, such as F:text, F:File. Three categories are defined: typed, untyped, character type and some Delphi file operation functions. For example: AssignPRn, Writeln, these file classes are similar to Windows File handle is incompatible
2. Windows file handle (handle)
The object-oriented Pascal file handle encapsulates the Windows file handle type, and the file operation function library encapsulates the Windows API function. For example, "Fileread" calls the Windows API function "ReadFile". Delphi provides a Windows API operation interface. If you are familiar with Windows API, you can use Windows file statements to perform file operations.
3. File Streams
The file stream is an object instance of the TFileStream class. The file stream is a high-level file operation type. TFileStream provides a handle attribute. This attribute can be used to operate Windows file handle types.
How to choose a file type
Windows file handle is a lower-level file operation type that provides flexible synchronous and asynchronous file read and write control. The following provides a pseudo-code description of file synchronization and asynchronous operation using Windows file handle type:
Synchronous operations:
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL);
// check for eof
if (bResult && nBytesRead == 0, ) {
// we're at the end of the file
}
Asynchronous operations:
// set up overlapped structure fields
gOverLapped.Offset = 0;
gOverLapped.OffsetHigh = 0;
gOverLapped.hEvent = NULL;
// attempt an asynchronous read Operation
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead,
&gOverlapped) ;
// if there was a problem, or the async. operation's still pending ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
//during the call to ReadFile
// code to handle that
}
case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress
// do something else for a while
GoDoSomethingElse() ;
// check on the results of the asynchronous read
bResult = GetOverlappedResult(hFile, &gOverlapped,
&nBytesRead, FALSE) ;
// if there was a problem ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
file during asynchronous operation
}
// deal with other error cases
}
}
} // end case
// deal with other error cases
} // end switch
} // end if
Although Windows file handles provide flexible file control, more error handling code must be written.
If you are not familiar with Windows API, use the old file variable type recommended by Delphi.
Delphi's old file type uses AssignFile to associate file variables with physical files, defined by Delphi
Various operations on file variables to complete file access and operation. Easy to use. The following provides the file variable class
Type operation code description:
var
F: TextFile;
S: string;
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
AssignFile(F, OpenDialog1.FileName); { File selected in dialog box }
Reset(F);
Readln(F, S); { Read the first line out of the file }
Edit1.Text := S; { Put string in a TEdit control }
CloseFile(F);
end;
end;
File stream is a subclass of stream (stream classes), so one advantage of using it is that it can automatically inherit the properties of its parent class. It can easily interoperate with other stream classes. For example, if you want to write a dynamic memory block Into the disk, you can use a TFileStream and a TMemoryStream to complete.