Difference between revisions of "File Handling In Pascal/zh TW"
Line 1: | Line 1: | ||
{{File Handling In Pascal}} | {{File Handling In Pascal}} | ||
− | + | 大概所有程式設計師最需要知道的就是如何處理檔案。檔案可以用來儲存使用者的設定,錯誤報告,或更多更多。在這裡我們將教您一些最基本的文字檔案處理。 | |
− | = | + | =舊方法= |
− | + | 當使用傳統非物件導向的 Pascal 時,你可以使用 TextFile 類型 (type),可以讓你建立檔案並對它寫入字串。 | |
<Delphi>... | <Delphi>... | ||
type | type | ||
− | TIntegerFile = file of Integer; // | + | TIntegerFile = file of Integer; // 可以讓你對檔案寫入整數值 |
− | TPCharFile = file of PChar; // | + | TPCharFile = file of PChar; // 將字元 (PChars) 寫入檔案 :\ |
− | TStringFile = file of string; // | + | TStringFile = file of string; // 將字串寫入檔案 |
...</Delphi> | ...</Delphi> | ||
− | + | 若我們僅用了 TStringFile = File,它將無法寫入任何東西!更無法將整數值寫入 TStringFile,因為它已經是字串型態的檔案。最好是使用 TextFile 的型態,它才能寫入各種不同形態的內容。 | |
− | == | + | ==輸入輸出== |
− | IO | + | IO 是 Pascal 專門用來處理檔案用的。他通常用來對付錯誤訊息。 |
− | + | 當用來當編譯器的指令時,你可以這麼用: | |
− | <Delphi>{$I-} // | + | <Delphi>{$I-} // 關閉檢查。這會使所有錯誤回報轉向存在 IOResult 這個變數裡 |
− | {$I+} // | + | {$I+} // 開啟回來</Delphi> |
− | + | 當關閉 IO 時他所有的輸出都會轉到 IOResult 變數裡。他是主要數字型態的變數 (cardinal type; Numbers)。所以如果你想要對它做寫入,你得使用 IntToStr 函式。不同的數字代表不同的錯誤。你可以參看這裡來了解每個不同的錯誤:[http://www.efg2.com/Lab/Library/Delphi/IO/IOResult.htm] | |
− | == | + | ==處理檔案的程序== |
− | + | 以下列出所有系統裡用來做檔案處理時會用的程序和函式。 | |
− | * '''Assign''' - | + | * '''Assign''' - 指派名稱給檔案 |
− | * '''Append''' - | + | * '''Append''' - 開啟已經存在的檔案,在檔案的末端接續額外內容並編輯它 |
− | * '''BlockRead''' - | + | * '''BlockRead''' - 對未定型態的檔案將其內容讀入到記憶體 |
− | * '''BlockWrite''' - | + | * '''BlockWrite''' - 將記憶體中的內容寫入未定型態的檔案 |
− | * '''Close''' - | + | * '''Close''' - 關閉開啟中的檔案 |
− | * '''EOF''' - | + | * '''EOF''' - 檢查是否為檔案末端 |
− | * '''Erase''' - | + | * '''Erase''' - 將檔案從磁碟上刪除 |
− | * '''FilePos''' - | + | * '''FilePos''' - 取得檔案定位 |
− | * '''FileSize''' - | + | * '''FileSize''' - 取得檔案大小 |
− | * '''Flush''' - | + | * '''Flush''' - 將檔案暫存寫入磁碟 |
− | * '''IOResult''' - | + | * '''IOResult''' - 回傳最後一個檔案 IO 操作的結果 |
− | * '''Read''' - | + | * '''Read''' - 將文字檔寫入變數 |
− | * '''ReadLn''' - | + | * '''ReadLn''' - 將文字檔寫入變數並換行 |
− | * '''Reset''' - | + | * '''Reset''' - 開啟一個檔案準備讀取 |
− | * '''Rewrite''' - | + | * '''Rewrite''' - 開啟一個檔案準備寫入 |
− | * '''Seek''' - | + | * '''Seek''' - 變更檔案中的定位 |
− | * '''SeekEOF''' - | + | * '''SeekEOF''' - 將檔案定位指到其結尾處 |
− | * '''SeekEOLn''' - | + | * '''SeekEOLn''' - 將檔案定位指到該行結尾處 |
− | * '''Truncate''' - | + | * '''Truncate''' - 於檔案定位處將內容清空 |
− | * '''Write''' - | + | * '''Write''' - 將變數內容寫入檔案 |
− | * '''WriteLn''' - | + | * '''WriteLn''' - 將變數內容寫入檔案並換行 |
Revision as of 10:05, 19 December 2010
│
العربية (ar) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
中文(中国大陆) (zh_CN) │
中文(台灣) (zh_TW) │
大概所有程式設計師最需要知道的就是如何處理檔案。檔案可以用來儲存使用者的設定,錯誤報告,或更多更多。在這裡我們將教您一些最基本的文字檔案處理。
舊方法
當使用傳統非物件導向的 Pascal 時,你可以使用 TextFile 類型 (type),可以讓你建立檔案並對它寫入字串。
<Delphi>... type
TIntegerFile = file of Integer; // 可以讓你對檔案寫入整數值 TPCharFile = file of PChar; // 將字元 (PChars) 寫入檔案 :\ TStringFile = file of string; // 將字串寫入檔案
...</Delphi>
若我們僅用了 TStringFile = File,它將無法寫入任何東西!更無法將整數值寫入 TStringFile,因為它已經是字串型態的檔案。最好是使用 TextFile 的型態,它才能寫入各種不同形態的內容。
輸入輸出
IO 是 Pascal 專門用來處理檔案用的。他通常用來對付錯誤訊息。 當用來當編譯器的指令時,你可以這麼用: <Delphi>{$I-} // 關閉檢查。這會使所有錯誤回報轉向存在 IOResult 這個變數裡 {$I+} // 開啟回來</Delphi>
當關閉 IO 時他所有的輸出都會轉到 IOResult 變數裡。他是主要數字型態的變數 (cardinal type; Numbers)。所以如果你想要對它做寫入,你得使用 IntToStr 函式。不同的數字代表不同的錯誤。你可以參看這裡來了解每個不同的錯誤:[1]
處理檔案的程序
以下列出所有系統裡用來做檔案處理時會用的程序和函式。
- Assign - 指派名稱給檔案
- Append - 開啟已經存在的檔案,在檔案的末端接續額外內容並編輯它
- BlockRead - 對未定型態的檔案將其內容讀入到記憶體
- BlockWrite - 將記憶體中的內容寫入未定型態的檔案
- Close - 關閉開啟中的檔案
- EOF - 檢查是否為檔案末端
- Erase - 將檔案從磁碟上刪除
- FilePos - 取得檔案定位
- FileSize - 取得檔案大小
- Flush - 將檔案暫存寫入磁碟
- IOResult - 回傳最後一個檔案 IO 操作的結果
- Read - 將文字檔寫入變數
- ReadLn - 將文字檔寫入變數並換行
- Reset - 開啟一個檔案準備讀取
- Rewrite - 開啟一個檔案準備寫入
- Seek - 變更檔案中的定位
- SeekEOF - 將檔案定位指到其結尾處
- SeekEOLn - 將檔案定位指到該行結尾處
- Truncate - 於檔案定位處將內容清空
- Write - 將變數內容寫入檔案
- WriteLn - 將變數內容寫入檔案並換行
Example
A full example of handling a text file of type TextFile:
<Delphi>program FileTest;
{$mode objfpc} // Do not forget this ever
uses
Sysutils;
var
FileVar: TextFile;
begin
WriteLn('File Test'); AssignFile(FileVar, 'Test.txt'); // You do not have to put .txt but this is just for now {$I-} try Rewrite(FileVar); // creating the file Writeln(FileVar,'Hello'); except Writeln('ERROR! IORESULT: ' + IntToStr(IOResult)); end; CloseFile(FileVar); ReadLn;
end.</Delphi>
Now open the file in any text editor and you will see Hello written to it!
Heres appending to a file(Editing it)
<Delphi>program EditFile;
{$mode objfpc}
uses
Sysutils;
var
File1: TextFile;
begin
WriteLn('Append file'); AssignFile(File1, 'File.txt'); {$I-} try Append(File1, 'Some Text'); except Writeln('ERROR IORESULT:' + IntToStr(IOResult)); end; {$I+} CloseFile(File1); Readln;
end.</Delphi>
Reading a file:
<Delphi>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); // Reads the whole line from the file Writeln(Str); // Writes the line read until(EOF(File1)); // EOF(End Of File) The the program will keep reading new lines until there is none. except Writeln('ERROR IORESULT:', IOResult); end; {$I+} CloseFile(File1); Readln;
end.</Delphi>
It is possible to do some file handling using chars instead of strings. This makes it look cool :D.
Object style
Most of string handling classes have ability to load and save content from/to file. These methods are usually named SaveToFile and LoadFromFile.
Binary files
For opening files for direct access TFileStream should be used. This class is encapsulation for system procedures FileOpen, FileCreate, FileRead, FileWrite, FileSeek and FileClose which resides in unit FileUtil. This class is basically platform independent as these procedures have specific implementation for each platform.
<Delphi>var
Buffer: array[0..10000] of Byte;
begin
with TFileStream.Create('SomeFile.bin', fmCreate) do try Seek('Hello'); Write(Buffer, SizeOf(Buffer)); finally Free; end;
end;</Delphi>
You can load entire file to memory too if it's size is comparatively smaller than available system memory.
<Delphi>begin
with TMemoryStream.Create do try LoadFromFile('SomeFile.bin'); Seek(0, soEnd); Write(Ord('A'), 1); SaveToFile('SomeFile.bin'); finally Free; end;
end;</Delphi>
Text files
In general for text files you can use class TStringList for loading entire file to memory and have simple access their rows.
<Delphi>begin
with TStringList.Create do try Add('Hello'); SaveToFile('SomeFile.txt'); finally Free; end;
end;</Delphi>