国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Delphi > 正文

Delphi中用于讀寫(I/O)的三種文件類型

2019-11-18 18:59:29
字體:
來源:轉載
供稿:網友

Delphi中用于讀寫(I/O)的三種文件類型

一.舊pascal文件類型
    用舊文件變量表示的文件類型,比如 F:text,F:File. 定義了三類:有類型,無類型,字符類型以及一些Delphi的文件操作函數.比如:AssignPRn,Writeln,這些文件類和Windows文件句柄不兼容

二.Windows文件句柄(handle)
    面向對象的Pascal的文件句柄封裝了Windows文件句柄類型,文件操作函數庫則封裝了Windows API函數,比如"Fileread"就是調用了Windows API 函數"ReadFile",Delphi提供了一個Windows API操作接口如果熟悉Windows API,可以用Windows文件句進行文件操作.

三.文件流(File Streams)
    文件流是TFileStream類的對象實例,文件流是高層的文件操作類型,TFileStream提供了一個句柄屬性.用此屬性可操作Windows文件句柄類型.

如何選擇文件類型

      Windows文件句柄是較底層的文件操作類型,提供了靈活的同步及異步文件讀寫控制,以下提供用Windows文件句柄類型對文件同步及異步操作的偽代碼描述:
同步操作:
bResult = ReadFile(hFile, &inBuffer, nBytesToRead, &nBytesRead, NULL) ;
// check for eof
if (bResult &&  nBytesRead == 0, ) {
 // we're at the end of the file
 }

異步操作:
// 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
 
雖然Windows文件句柄提供靈活的文件控制,但須編寫更多的出錯處理代碼,如果對
WindowsAPI不熟悉,使用Delphi推薦的舊文件變量類型.

       Delphi的舊文件類型使用AssignFile,使文件變量和物理文件關聯,通過Delphi定義的
對文件變量的各種操作,完成文件的存取和操作.使用方便.以下提供對文件變量類
型的操作代碼描述:
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;

      文件流是流(stream classes)的子類,所以使用他的一個優點就是能自動繼承其父類的屬性他能很容易的和其他的流類互操作,比如你如果想把一塊動態內存塊寫入磁盤,可以使用一個TFileStream和一個TMemoryStream來完成.


上一篇:Delphi嵌入式匯編一例

下一篇:如何在Delphi中實現ASP編程

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 安新县| 黔西县| 广东省| 宣汉县| 皋兰县| 德阳市| 周口市| 桦甸市| 瓮安县| 育儿| 昌邑市| 桦甸市| 南通市| 乳山市| 当雄县| 资讯 | 宁城县| 通渭县| 博客| 东莞市| 长兴县| 集贤县| 黑山县| 平定县| 阿合奇县| 黄骅市| 舟曲县| 建平县| 义马市| 贵定县| 克拉玛依市| 禹城市| 白河县| 临泉县| 新邵县| 都兰县| 武陟县| 彰武县| 江永县| 二连浩特市| 喜德县|