Three file types used to read and write (IO) in Delphi

zhaozj2021-02-08  274

Three file types used to read and write (I / O) in Delphi

I. Old Pascal file type The file type represented by the old file variable, such as F: text, f: file. Define three categories: Type, no type, character type, and some Delphi file operation function. For example: AssignPRN, WriteLn These file classes are not compatible with Windows file handles.

II.Windows file handle (HANDLE) Object-oriented PasCal file handle packages the Windows file handle type, file operation function library encapsulates the Windows API function, such as "fileRead" is called the Windows API function "Readfile", Delphi provides A Windows API Operating Interface If you are familiar with the Windows API, you can use the Windows file to perform file operations.

3. File Streams file stream is an object instance of the TFileStream class. The file stream is a high-level file operation type, and TFileStream provides a handle property. Use this property to operate the Windows file handle type.

How to choose a file type

The Windows file handle is a lower file operation type, providing flexible synchronization and asynchronous file read and write control, the following provides a pseudo code description for file synchronization and asynchronous operation with Windows file handle type: Synchronization operation: 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 is 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 godosomethinge (); // check on the results of the asynchronous read bresult = getoverlappedResult (Hfile, & Goverlapped,

& nbytesread, false; // if there is a problem ... if (! BRESULT) {// DEAL with the error code switch (dwerror = getLastError ()) {copy error_handle_eof: {// We're Reached The end of The File File During Asynchronous Operation} // DEAL WITHER ERROR CASES}}} // end case} // end switch} // end if, although the Windows file handle provides flexible file control, Write more error handling code, if you are not familiar with WindowsAPI, using the old file variable type recommended by Delphi.

Delphi's old file type uses AssignFile to associate file variables and physical files, through Delphi's various operations of file variables, complete file access and operation. Easy to use. Description of operation code for file variable types: VAR

F: textfile; s: string; begin if OpenDialog1.execute kiln} becom 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;

The file stream is a subclass of stream classes, so a advantage of using him is that it can automatically inherit its parent class. He can easily and other streaming interoperability, such as if you want to write a dynamic memory block. Enter the disk, you can use a TFileStream and a TMEMORYSTREAM to complete.

转载请注明原文地址:https://www.9cbs.com/read-2601.html

New Post(0)