Text/ja
TextFile変数(古い形式のTextに相当する)は、テキストファイルの読み書きに用います。
{$mode objfpc}{$H+}
var
MyFile: TextFile;
s: string;
begin
AssignFile(MyFile, 'a.txt');
try
reset(MyFile);
readln(MyFile, s);
writeln('Text read from file: ', s)
finally
CloseFile(MyFile)
end
end.テキストファイルを表現する変数(上記例ではMyFile)によって、実際のテキストファイルに対する読み込みおよび書き込みを実行できます。テキストファイル変数は、run-time libraryのAssignFileを用いて実際のファイルと関連付ける必要があります。そして、ファイルを開くにはResetやRewrite、またはAppendプロシージャを用います。ファイルの読み書きには、Read、Readln、Write、Writelnを使います。ファイルの処理が終わったら、ファイルリソースを解放するためにCloseFileを呼び出して、ファイルをクローズする必要があります。
注意:TextFile変数は、file of charの変数とは全く異なります。:
- file of char は単純なバイトキャラクターの連続であり、1回1バイトの読み書きとなります。つまり、「C」 がcharタイプの変数とすると、Read(F, C) か Write(F, C) のみが使えるということです。
- 一方、TextFileを用いることで、一般的なテキストファイル向けの機能が使えるようになります。例えば、「Read」や「Readln」、「Write」や「Writeln」を用いて、テキストファイルに標準的な変数(stringやinteger、float)を読み書きできます。また、行末を自動的に処理できます。つまり、読むときは各種の行末を自動的に認識し、書くときは使用しているOSの行末コードを書き込みます。
From the Free Pascal wiki, page "Text/ja", as it stood on 2020-02-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
