Pascal文件处理
关键字:pascal、file、TFileStream、TMemoryStream、TStringList
大多数程序员都需要知道如何操作文件。文件可以用来保存用户设置、错误日志等等。在这里我将教你如何操作文本文件。
1.标准Pascal文件
使用标准Pascal文件(非面向对象),你可以使用一个文本文件类型,它允许你将字符串写入到文件或创建自己的文件类型。
...
type
TIntegerFile = file of Integer; // 允许你将整数写入文件
TPCharFile = file of PChar; // PChars写入到文件
TStringFile = file of String; // 字符串写入到文件
...
1.1 IO
它告诉编译器如何处理 IO 错误:抛出一个异常或将结果存储到 IOResult 变量。 因为它是一个编译器指令,所以:
{$I-} // 关闭检查。将所有的错误存入IOResult变量
{$I+} // 把它重新打开,将导致EInOutError异常
通过禁用/关闭 $I, 文件操作结果进入 IOResult 变量。这是一个基数数字类型。 所以,如果你想显示 IOResult,你必须使用 IntToStr 功能。不同的数字代表不同的错误。所以你可能要检查文档中不同的错误。
1.2 文件程序
这些文件处理过程和函数位于系统单元。
- AssignFile(或者旧的Assign) - 将文件名赋给变量名
- Append - 以附加的方式打开已有的文件
- BlockRead - 读一个或多个记录到变量中
- BlockWrite - 从变量中写一个或多个记录
- CloseFile(或者旧的Close) - 关闭打开的文件
- EOF - 测试文件是否到文件尾
- Erase - 删除文件
- FilePos - 返回文件的当前指针位置
- FileSize - 返回当前文件的大小
- Flush - 将缓冲区的内容刷新到输出的文本文件中
- IOResult - 返回最新的I/O操作完成状态
- Read - Read from a text file into variable(从文本文件到变量)
- ReadLn - Read from a text file into variable and goto next line(读取文本文件并转到下一行)
- Reset - Opens a file for reading(打开文件并读取)
- Rewrite - Open file for writing(打开并写入文件)
- Seek - Change position in file(更改文件指针位置)
- SeekEOF - Set file position to end of file(设置文件位置为结尾)
- SeekEOLn - Set file position to end of line(文件位置设置为行结束)
- Truncate - Truncate the file at position(截断文件位置)
- Write - Write variable to a text file(写入变量到文本文件)
- WriteLn - Write variable to a text file and append newline(写入新行到文件)
1.3 示例
一个完整的处理文本文件的示例:
program FileTest;
{$mode objfpc} // 不要忘了这个
uses
Sysutils;
var
FileVar: TextFile;
begin
WriteLn('File Test');
AssignFile(FileVar, 'Test.txt'); // 你不需要输出 .txt 但现在需要
{$I+} // 使用异常处理
try
Rewrite(FileVar); // 创建文件
Writeln(FileVar,'Hello');
// Use CloseFile rather than Close as Close is used in other units as well
CloseFile(FileVar);
except
on E: EInOutError do
begin
Writeln('File handling error occurred. Details: '+E.ClassName+'/'+E.Message);
end;
end;
WriteLn('Program finished. Press enter to stop.');
ReadLn;
end.
任意编辑器打开这个文件,你将看到Hello ,你可以设置 Test.txt 文件为只读,再运行程序。这时程序会抛出异常,显示错误消息。
注: 我们使用异常处理({$I+}),它是对多个文件操作及处理错误时的一个简单方法。你也可以使用{$I-},但你必须在每次操作后检查但你必须检查 IOResult,并修改你的下一步操作。
以下是如何追加/添加到文件的示例:
program EditFile;
{$mode objfpc}
uses
Sysutils;
var
File1: TextFile;
begin
WriteLn('Append file');
{$I+}
try
AssignFile(File1, 'File.txt');
{
我这里测试时,原版示例不能执行报错 Error: Wrong number of parameters specified for call to "Append"
在Lazarus 1.2.2,FPC2.6.4上
}
// Append(File1, 'adding some text...');
{ 修改后的程序 }
Append(File1);
Writeln(FileVar,'adding some text...');
Writeln
CloseFile(File1);
except
on E: EInOutError do
begin
Writeln('File handling error occurred. Details: '+E.ClassName+'/'+E.Message);
end;
end;
WriteLn('Program finished. Press enter to stop.');
Readln;
end.
读取文件:
program ReadFile;
{$mode objfpc}
uses
Sysutils;
var
File1: TextFile;
Str: String;
begin
Writeln('File Reading:');
AssignFile(File1, 'File.txt');
{$I+}
try
Reset(File1);
repeat
Readln(File1, Str); // 从文件中读取一行
Writeln(Str); // 显示这行
until(EOF(File1)); // EOF(文件结束)程序将继续读取直到文件结尾。
CloseFile(File1);
except
on E: EInOutError do
begin
Writeln('File handling error occurred. Details: '+E.ClassName+'/'+E.Message);
end;
end;
WriteLn('Program finished. Press enter to stop.');
Readln;
end.
2.面向对象
除了上面的标准文件处理例程,在系统中,更高的抽象层次里使用流(数据流)的概念,这意味着作为一个程序员在处理文件时执行更少的步骤。
此外,大部分字符串处理类可以加载(保存)内容从(到)文件。这些方法通常是 SaveToFile 和 LoadFromFile。很多其他对象(像Lazarus网格)也有类似的功能,包括 Lazarus 数据集(DBExport)。
2.1 二进制文件
为操作文件应该使用 TFileStream。这个类封装了系统程序 FileCreate、FileRead、FileWrite、FileSeek 和 FileClose,它在 FileUtil 单元。 在下面的例子中,注意我们是如何在 try... finally 块中封装处理文件。这可以确保文件流对象总是释放(在finally... 部分),即使有文件访问(或其他)错误。
Buffer: array[0..9999] of Byte;
begin
with TFileStream.Create('SomeFile.bin', fmCreate) do
try
Seek('Hello');
Write(Buffer, SizeOf(Buffer));
finally
Free;
end;
end;
你可以加载整个文件到内存中,如果文件大小大于系统可用内存,你的操作系统会开始使用页面/交换文件。
begin
with TMemoryStream.Create do
try
LoadFromFile('SomeFile.bin');
Seek(0, soEnd);
Write(Ord('A'), 1);
SaveToFile('SomeFile.bin');
finally
Free;
end;
end;
对于大文件:
var
TotalBytesRead, BytesRead : Int64;
Buffer : array [0..4095] of byte; // 或 array [0..4095] 为 char
FileStream : TFileStream;
try
FileStream := TFileStream.Create;
FileStream.Position := 0; // 确保在文件开始位置
while TotalBytesRead <= FileStream.Size do // 当总读取大小 <= 文件大小
begin
BytesRead := FileStream.Read(Buffer,sizeof(Buffer)); // 读取 4096 字节数据
inc(TotalBytesRead, BytesRead); // 增加 TotalByteRead 缓冲区的大小,也就是说 4096 字节
// 做一些与缓冲数据相关的操作
end;
示例:复制文件,我们可以实现一个简单的 FileCopy 功能。
function FileCopy(Source, Target: string): boolean;
// 复制文件 source 到 target;覆盖目标文件
// 在内存中缓存整个文件
// 成功返回 true,失败返回false
var
MemBuffer: TMemoryStream;
begin
result:=false;
MemBuffer:=TMemoryStream.Create;
try
try
MemBuffer.LoadFromFile(Source);
MemBuffer.Position:=0;
MemBuffer.SaveToFile(Target); //可能是源文件相同
result:=true;
except
result:=false; //忽略异常,转换为错误代码
end;
finally
MemBuffer.Free;
end;
end;
2.2 文本文件
一般情况下,对于文本文件可以使用 TStringList 类将整个文件加载到内存中,并可以对行进行简单存取。当然,你也可以保存 StringList 到文件:
begin
with TStringList.Create do
try
Add('Hello');
SaveToFile('SomeFile.txt');
finally
Free;
end;
end;
为了写单个字符串到流可能需要使用以下过程:
procedure SaveStringToPath(theString, filePath: String);
var
textFile: TFileStream = nil;
textLength: integer;
stringBuffer: ^String;
begin
textLength := length(theString);
try
textFile := TFileStream.Create(filePath, fmOpenWrite or fmCreate);
{ write string to stream while avoiding to write the initial length }
{ 写字符串到流,同时避免写入超过最初长度 }
stringBuffer := @theString + 1;
textFile.WriteBuffer(stringBuffer^, textLength);
finally
if textFile <> nil then textFile.Free;
end;
end;
2.3 File
在Pascal程序中,file 类型的变量可用于读取、写入或同时读取和写入文件。文件变量(通常)使用 AssignFile 过程与磁盘上的文件相关联。
2.3.1 File types
1. 非类型化二进制文件
非类型化意味着它只是一个数据序列(字节)。它可以用来存储基本上任何东西。它可以通过 BlockRead 读取,也可以通过 BlockWrite 程序写入。例子:
{$mode objfpc}{$H+}
var
MyFile: file;
Data: array [0..99] of Byte;
begin
AssignFile(MyFile, 'a');
Reset(MyFile, 1 { size of read chunk });
try
BlockRead(MyFile, Data, SizeOf(Data));
finally
CloseFile(MyFile);
end;
end.
2. 二进制文件
file of <type>
其中<type>是任何简单类型(没有引用/指针)或记录类型,是表示<type>值序列的二进制文件。例如,您可以拥有一系列整数、浮点值或记录的文件(只要所有记录字段也是简单类型)。
{$mode objfpc}{$H+}
var
MyFile: file of Single;
Data: Single;
begin
AssignFile(MyFile, 'singlevalues.bin');
try
Reset(MyFile);
Read(MyFile, Data);
finally
CloseFile(MyFile);
end;
end.
3. 文本文件
类型 TextFile(或者等效名称:Text)表示文本文件。注意,这不仅仅是字符文件。文本文件有各种舒适的读写操作,可以解析整数、浮点值和其他类型。此外,当您使用文本文件时,可以正确处理行尾。
示例:
{$mode objfpc}{$H+}
var
MyFile: TextFile;
s: string;
begin
AssignFile(MyFile, 'a.txt');
try
reset(MyFile); //Reopen the file for reading
readln(MyFile, s);
writeln('Text read from file: ', s)
{
or add some text:
append(MyFile);
writeln(MyFile, 'some text');
}
finally
CloseFile(MyFile)
end
end.
在上面的示例中 MyFile 表示文本文件的变量,可用于读取、写入或同时写入实际文件。它必须通过运行时库例程AssignFile 绑定到实际文件。然后,必须通过重置、重写或追加过程打开文件。可以使用 read、Readln、write、Writeln 对文件进行读写。处理完文件后,应通过关闭调用 CloseFile 的文件来释放必要的文件资源。
注意,TextFile类型与char类型的文件非常不同:
char文件只是一个简单的单字节字符序列,一次只能读写一个字符。也就是说,您只能调用Read(F,C)或Write(F,C),其中C是char类型的变量。
TextFile 提供了更多的功能,并代表了文本文件的常见概念。可以使用 Read、Readln、Write、Writeln 从文本文件中读取/写入许多标准类型,如字符串、整数和浮点值。行尾也会自动处理:读取时,会识别各种行尾;写入时使用当前OS行结尾。
2.3.2 使用文件
使用所有文件类型都是类似的:
1.将文件与数据关联。通常通过调用 AssignFile。一个旧的等效名称是 Assign。(随着GUI库的出现,出现了与一些传统 Pascal 文件例程的名称冲突的可能性。TPersistent 和TForm(TPersisten派生)具有 Assign 和 Close 方法。因此,如果在 TForm 方法中编写 Assign 或 Close,可能会产生混淆,因此 AssignFile 和 CloseFile 被引入为传统 Assign 和 Close 的别名,以防止无意中调用意外的过程。在非 GUI 代码中,您当然可以使用原始的 Assign 和 Close,并节省一些输入)。
2.通过“Reset”(读取现有文件,并可能写入——见下文)、“Rewrite”(清除内容并写入)或“Append”(保留内容并写入”)打开文件。如果在非类型二进制文件上重置,全局F ileMode 变量将确定您是只能读取(fmOpenRead)文件,还是同时读取和写入(fmOpenReadWrite)文件。
3.然后通过 read、readln、write和writeln 标准过程读取/写入文件数据。一些Pascal 编译器还为 I/O 使用 Get 和 Put 例程。对于非类型二进制文件,请使用 BlockRead 和 BlockWrite。
4.最后,通过调用 CloseFile(旧的,等效名称:just Close)释放必要的文件资源(并可能刷新要写入的数据)。
文件变量用于输入、输出或输入和输出(I/O)。文件可以是屏幕和/或键盘、调制解调器、光笔、触摸屏、鼠标、磁盘上的存储区域、用于连接到 Internet 的插座,也可以是其他设备,具体取决于运行时库如何绑定文件。在某些情况下,实际内存可以定义为文件;这是在 ram 磁盘的情况下使用的。您可以通过初始化 FileRec 记录(用于非类型二进制文件)或 TextRec 记录,而不是调用标准的 AssignFile(Assign)例程来自定义文件数据的位置。