Text/ja: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Text/ja
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TextFile**変数(古い形式の**Text**に相当する)は、テキストファイルの読み書きに用います。 | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | {$mode objfpc}{$H+} | |
| 5 | + | var | |
| 6 | + | MyFile: TextFile; | |
| 7 | + | s: string; | |
| 8 | + | begin | |
| 9 | + | AssignFile(MyFile, 'a.txt'); | |
| 10 | + | try | |
| 11 | + | reset(MyFile); | |
| 12 | + | readln(MyFile, s); | |
| 13 | + | writeln('Text read from file: ', s) | |
| 14 | + | ||
| 15 | + | finally | |
| 16 | + | CloseFile(MyFile) | |
| 17 | + | end | |
| 18 | + | end. | |
| 19 | + | ``` | |
| 20 | + | ||
| 21 | + | テキストファイルを表現する[[Variable|変数]](上記例では*MyFile*)によって、実際のテキストファイルに対する読み込みおよび書き込みを実行できます。テキストファイル変数は、[[RTL|run-time libraryの]][[AssignFile|AssignFileを]]用いて実際のファイルと関連付ける必要があります。そして、ファイルを開くには[[Reset|Resetや]][[Rewrite]]、または[[Append|Appendプロシージャを]]用います。ファイルの読み書きには、[[Read]]、[[Readln]]、[[Write]]、[[Writeln|Writelnを]]使います。ファイルの処理が終わったら、ファイルリソースを解放するために[[CloseFile|CloseFileを]]呼び出して、ファイルをクローズする必要があります。 | |
| 22 | + | ||
| 23 | + | 注意:**TextFile**変数は、**file of char**の変数とは全く異なります。: | |
| 24 | + | ||
| 25 | + | - **file of char** は単純なバイトキャラクターの連続であり、1回1バイトの読み書きとなります。つまり、「C」 が**char**タイプの変数とすると、**Read(F, C)** か **Write(F, C)** のみが使えるということです。 | |
| 26 | + | ||
| 27 | + | - 一方、**TextFile**を用いることで、一般的なテキストファイル向けの機能が使えるようになります。例えば、「Read」や「Readln」、「Write」や「Writeln」を用いて、テキストファイルに標準的な変数(stringやinteger、float)を読み書きできます。また、[[End of Line|行末を]]自動的に処理できます。つまり、読むときは各種の行末を自動的に認識し、書くときは使用しているOSの行末コードを書き込みます。 | |
| 28 | + | ||
| 29 | + | --- | |
| 30 | + | *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](/wiki-contact).* |
