Delphi中用于读写(I/O)的三种文件类型
2000-12-12 14:03
543 查看
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来完成.
一.旧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中用于读写(I/O)的三种文件类型
- delphi中无类型文件读写
- delphi中无类型文件读写
- delphi中无类型文件读写
- 实现快速读写配置文件的内容,可以用于读取*.exe.config文件或者Web.Config文件的内容,或者可以读取指定文件的配置项.
- 8.9 编写函数打开文件用于输入,将文件内容读入 string 类型的 vector 容器,每一行存储为该容器对象 的一个元素。8.10 重写上面的程序,把文件中的每个单词存储为 容器的一个元素
- DELPHI 文件类型种类
- java实现对properties类型文件的读写
- Delphi中实现文件拷贝的三种方法
- 用ASP.net判断上传文件类型的三种方法
- ASP.net 判断上传文件类型的三种方法
- delphi编程如何判断图片文件的真实类型?
- 20110327-8.1-fstream定义读写已命名文件的类型
- Delphi读写二进制文件
- 在Delphi中读写TXT文件
- 用ASP.net判断上传文件类型的三种方法
- delphi中转换office word文件为HTML文件,或者其它类型的文件的一些总结
- 删除一个目录下部分类型之外的所有文件的三种方法
- ArcGIS 网络分析[1.1] 创建用于网络分析用的线类型shp文件[这个太基础了吧!]
- Delphi 7使用自定义图标关联文件类型