Explain the type of file used for reading and writing in Delphi

Source: Internet
Author: User
Tags error code readfile

I. Old Pascal file type

A file type, such as F:text,f:file, that is represented by a file variable. Defines three classes: there are types, no types, character types, and some Delphi file manipulation functions. For example: Assignprn,writeln, these file classes are incompatible with Windows file handles

Two. Windows file handle (handle)

Object-oriented Pascal's file handle encapsulates the Windows file handle type, and the file Manipulation function library encapsulates Windows API functions, such as "Fileread", which calls the Windows API number "ReadFile". Delphi provides a Windows API operator interface if you are familiar with the Windows API, you can use Windows file sentences for file operations.

Three. File Flow (Streams)

The file stream is an object instance of the TFileStream class, and the file stream is the top-level file-handling type, and TFileStream provides a handle property. Use this property to manipulate the Windows file handle type.

How to select a file type

Windows file handles are lower-level file operation types, providing flexible synchronous and asynchronous file read and write control, and the following provides pseudo code descriptions of file synchronization and asynchronous operations with Windows file handle types:

Sync Action:

bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL) ;
// check for eof
if (bResult && nBytesRead == 0, ) {
// we"re at the end of the file
}

Asynchronous Operation:

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 still in progress
Do something else for a while
Godosomethingelse ();
Check on the results of the asynchronous read
Bresu = 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 the other error cases
}
}
}//End case
Deal with the other error cases
}//End switch
}//End If

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.