File Handling In Pascal/ja

From Lazarus wiki
Revision as of 16:14, 21 March 2014 by TheCreativeCAT (talk | contribs) (→‎Object style: Translation added)
Jump to navigationJump to search

العربية (ar) English (en) español (es) suomi (fi) français (fr) 日本語 (ja) русский (ru) 中文(中国大陆)‎ (zh_CN) 中文(台灣)‎ (zh_TW)

ほとんどのプログラマにとって、ファイルの扱いは是非とも知らなければならないものです。ファイルはユーザの設定や、エラーのログや、その他諸々のものを保存するのに使えます。ここでは基本的なテクストファイルの扱い方を教えましょう。

オーソドックスな Pascal

Pascal は強固に型付けられた言語で、ファイル型もデータの型に file of を付けて作ります。

type
  fc = file of char;
  fi = file of integer;
  fr = file of real;

はそれぞれ、char 型、integer 型、real 型のデータを収めるファイルの宣言です。単純型だけではなく、配列型、レコード型、集合型といったユーザ定義の型のファイルも可能です。

テクスト型は文字からなるファイルですが、file of char や file of string とは異なった特徴をもっており、行の概念や文字列と数との間の自動変換などの機能を持っています。

テクスト型は、

Program TextWriteTest;
var t : text;
    i : integer;
begin
  assign(t, ファイル名);
  rewrite(t);
  for i := 1 to 100 do write(t, i : 4);
  writeln(t, ' は書式付出力の例');
  close(t)
end.

Program TextReadTest;
var t : text;
    i : integer;
    s : string;
begin
  assign(t, ファイル名);
  reset(t);
  for i := 1 to 100 do read(t, i);
  readln(t, s);
  close(t)
end.

のように用います。「ファイル名」のファイルをエディタその他で読むと、1から100までの数がならび、それぞれ四桁のスペースをとっているはずです。Free Pascal でコンソールアプリケーションを作成する場合はこのような伝統的なプログラミングが可能です。しかし Lazarus では assign, close, text といった識別子がウィジェットセットのプロパティ名やメソッド名として既に用いられていますので、system.text などと system ユニットの名前を明示するか、次に挙げられている TextFile 型などをお使いください。例えば TextFile 型は objpash.inc の中で TextFile = text として定義されており、AssignFile 手続きはユニット objpas で定義されていて、system.text を呼出します。

古い手続型スタイル

古典的な(オブジェクト指向ではない)Pascal では、TextFile 型を使って文字列を書き込むことができます。あるいは、自前のファイル型を定義することもできます。

...
type
 TIntegerFile = file of Integer; // Integer を書き込めます
 TPCharFile = file of PChar; // PChar を書き込めます
 TStringFile = file of String; // String を書き込めます
...

TStringFile = File とだけしてしまうと、何も書けません(訳注: バイト単位で読み書きする特殊なファイルになります)。TStringFile には整数型を直接書くこともできません。というのもそれは string のファイルだからです。異なる型のデータを書き込むには、TextFile 型がよいでしょう。

IO エラーの扱い

コンパイラに IO エラーの場合どう処理するか知らせることができます: 例外を発生するか、例外を発生せず変数 IOResult に結果を格納するかです。これはコンパイラディレクティブです:

{$I-} // チェックを off にする。エラーが起きたら、全て変数 IOResult に行く
{$I+} // チェックを on に戻す。エラーが起きると EInOutError 例外になる

$I をディセーブル/オフにすると、ファイル操作の結果は変数 IOResult に行きます。これは符号無し整数型です。エラーの種類が格納されます。対応表は: [1] にあります。

ファイル手続き

System ユニットに含まれるファイル関係の手続き及び関数です。詳細は: Reference for 'System' unit をご覧下さい。前述のように、Lazarus ではプロパティ名やメソッド名で隠蔽されるものがあります。

  • Assign - ファイルとファイル名とを関連づけます
  • Append - 既存のファイルを開き、その最後からデータを追加していきます。
  • BlockRead - ファイルを指定のバイト数一気に読み、メモリに内容を格納します。型無しファイルも可能です。
  • BlockWrite - メモリの内容を、指定のバイト数一気にファイルに書き出します。型無しファイルも可能です。
  • Close - 開いたファイルを閉めます。
  • EOF - ファイル末に達したか調べます。
  • EOLN - テクストファイルで行末に達したか調べます。
  • Erase - ディスクからファイルを消去します。
  • FilePos - ファイル内の読み/書きする位置を得ます。
  • FileSize - ファイルの大きさを得ます。
  • Flush - ファイルバッファに残ったデータをディスクに書き出します。
  • IOResult - 最後のディスク IO 操作の結果を格納します。
  • Read - 変数にファイルの内容を読み込みます。テクストファイル以外にも使えます。
  • ReadLn - テクストファイルから行末まで読み、次の行に行きます。
  • Reset - 読み込み用にファイルを開きます。
  • Rewrite - 書き出し用にファイルを開きます。
  • Seek - ファイル内の読み/書きする位置を変更します。
  • SeekEOF - 空白を読み飛ばしながらファイル末か調べます。
  • SeekEOLn - 空白を読み飛ばしながら行末か調べます。
  • Truncate - 現在の位置でファイルを切り捨てます。
  • Write - ファイルに変数の内容を書き出します。テクストファイル以外にも使えます。
  • WriteLn - テクストファイルに変数の内容を書き出し、改行します。

TextFile 型を扱う例です。この例自体はコンソールアプリケーションですが、Lazarus で GUI アプリケーションを作成する場合は、こちらの例に従ってください。

program FileTest;
{$mode objfpc} // コンパイラをオブジェクト Pascal モードにします。これを忘れないで

uses
 Sysutils;

var
 FileVar: TextFile;  // textの代わりにこれを使います。

begin
  WriteLn('File Test');
  AssignFile(FileVar, 'Test.txt'); // assignの代わりにこれを使います。.txt はなくても構いませんが、ここでは付けておきましょう
  {$I+} // IOエラーの際、例外を発生させます
  try  
    Rewrite(FileVar);  // ファイルを新規に作成します  
    Writeln(FileVar,'Hello');
    CloseFile(FileVar);  // closeの代わりにこれを使います。
  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 をチェックしなければなりません。そうしないと次の操作で IOResult の内容が変わってしまいます。

テクストファイルに追加する方法です:

program EditFile;
{$mode objfpc}

uses
 Sysutils;

var
 File1: TextFile;
 
begin
  WriteLn('Append file');
  {$I+}
  try
    AssignFile(File1, 'File.txt');
    Append(File1, 'adding some text...');
    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); // 行全体を変数 Str に読み込みます
      Writeln(Str); // 読んだ行を書きます。ファイル変数を指定しない場合は標準出力に書きます。
    until(EOF(File1)); // ファイルの最後に達するまで繰り返し新しい行を読みます。
    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.

場合によっては、string ではなく char を使ってファイルを扱うこともできます。カッコいいですね (^_-)

オブジェクトスタイル

上述の古いスタイルのファイル操作に加えて、stream (データストリーム)の概念に基づく抽象度の高いシステムが用意されています。これは、ファイルの扱いをもっと少ないステップで行えるということです。

加えて、文字列を扱うほとんどのクラスにファイルからの読み書き能力が備わっています。それらは通常 SaveToFile と LoadFromFile という名前になっています。Lazarus grid のような多くの他のオブジェクトにも同様の機能があります。Lazarus dataset (DBExport) もそうです。そのドキュメントやソースコードにはインポート/エクスポートルーチンに取り組む前に一読する価値があります。

Binary files

For opening files for direct access TFileStream should be used. This class is an encapsulation of the system procedures FileOpen, FileCreate, FileRead, FileWrite, FileSeek and FileClose which resides in unit FileUtil.

IO routines

In the example below, note how we encapsulate the file handling action with a try..finally block. This makes sure the Filestream object is always released (the finally... part) even if there are file access (or other) errors.

var
  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;


You can load entire files into memory too if its size is comparatively smaller than available system memory. Bigger sizes might work but your operating system will start using the page/swap file, making the exercise useless from a performance standpoint.

begin
  with TMemoryStream.Create do 
  try
    LoadFromFile('SomeFile.bin');
    Seek(0, soEnd);
    Write(Ord('A'), 1);
    SaveToFile('SomeFile.bin');
  finally
    Free;
  end;
end;

With larger files of many Gb, you may want to read in buffers of, say, 4096 bytes (you're advised to use a multiple of the filesytem cluster or block size) and do something with the data of each buffer read.

var
  TotalBytesRead, BytesRead : Int64;
  Buffer : array [0..4095] of byte;  // or, array [0..4095] of char
  FileStream : TFileStream;

try
  FileStream := TFileStream.Create;
  FileStream.Position := 0;  // Ensure you are at the start of the file
  while TotalBytesRead <= FileStream.Size do  // While the amount of data read is less than or equal to the size of the stream do
  begin
    BytesRead := FileStream.Read(Buffer,sizeof(Buffer));  // Read in 4096 of data
    inc(TotalBytesRead, BytesRead);                       // Increase TotalByteRead by the size of the buffer, i.e. 4096 bytes
    // Do something with Buffer data
  end;

FileCopy

With the above, we can implement a simple FileCopy function (FreePascal has none in its RTL although Lazarus has one) - adjust as needed for bigger files etc:

function FileCopy(Source, Target: string): boolean;
// Copies source to target; overwrites target.
// Caches entire file content in memory.
// Returns true if succeeded; false if failed
var
  MemBuffer: TMemoryStream;
begin
  result:=false;
  MemBuffer:=TMemoryStream.Create;
  try
    try
      MemBuffer.LoadFromFile(Source);
      MemBuffer.Position:=0;
      MemBuffer.SaveToFile(Target); //may be same as source
      result:=true;
    except
      result:=false; //swallow exception; convert to error code
    end;
  finally
    MemBuffer.Free;
  end;
end;

Text files

In general, for text files you can use the TStringList class to load the entire file into memory and have simple access to their lines. Of course, you can also save your StringList back to file:

begin
  with TStringList.Create do 
  try
    Add('Hello');
    SaveToFile('SomeFile.txt');
  finally
    Free;
  end;
end;

In order to write a single string to a stream you might want to use the following procedure:

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;

See also