Basic Pascal Tutorial/Chapter 2/Files/ja: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Basic Pascal Tutorial/Chapter 2/Files/ja
| Old | New | Line | |
|---|---|---|---|
| 1 | + | 2D - ファイル (著者: Tao Yue, 状態: 原文のまま修正なし) | |
| 2 | + | ||
| 3 | + | 画面(キーボード)からのかわりにファイルから読み込む場合には次のように行う。 | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | read (ファイル変数, 引数リスト); | |
| 7 | + | write (ファイル変数, 引数リスト); | |
| 8 | + | ``` | |
| 9 | + | ||
| 10 | + | `readln` と `writeln` と同様にファイル変数は以下のように宣言される。 | |
| 11 | + | ||
| 12 | + | ``` pascal | |
| 13 | + | var | |
| 14 | + | ... | |
| 15 | + | filein, fileout : text; | |
| 16 | + | ``` | |
| 17 | + | ||
| 18 | + | `text` というデータタイプはファイルが単なるテキストであることを示している。 | |
| 19 | + | ||
| 20 | + | ファイルのための変数を宣言した後、その変数への書き込み、あるいはその変数からの読み込みをする前に、ディスク上のファイル名と関連づけてファイルをオープンしなくてはならない。これには2つのやり方のいずれかを使う。典型的には次のようになる。 | |
| 21 | + | ||
| 22 | + | `reset (ファイル変数, 'ファイル名.拡張子');`\ | |
| 23 | + | `rewrite (ファイル変数, 'ファイル名.拡張子');` | |
| 24 | + | ||
| 25 | + | `reset` は読み込みのためにファイルをオープンし、 `rewrite` は書き込みのためにファイルをオープンする。`reset` でオープンされたファイルには `read` と `readln`だけが使える。 `rewrite` でオープンされたファイルには`write` と `writeln`だけが使える。 | |
| 26 | + | ||
| 27 | + | Turbo Pascal は割り当て表記 (assign notation) を導入した。最初にファイル名を変数に割り当てれば、その変数を使うだけで`reset` あるいは `rewrite` を呼び出せるのである。 | |
| 28 | + | ||
| 29 | + | ``` pascal | |
| 30 | + | assign (ファイル変数, 'ファイル名.拡張子'); | |
| 31 | + | reset (ファイル変数); | |
| 32 | + | ``` | |
| 33 | + | ||
| 34 | + | パスを表現する方法はオペレーティング・システムによって異なる。 Windows のユーザなら DOS の遺産のためにバック・スラッシュとドライブを表す文字を使うだろう (たとえば、 `c:\directory\name.pas`)。一方、 macOS と Linux では UNIX の遺産で普通のスラッシュ(forward slashes) を使うことになるだろう。 | |
| 35 | + | ||
| 36 | + | ファイルで作業をした後には、次のようにしてファイルを閉じることができる。 | |
| 37 | + | ||
| 38 | + | ``` pascal | |
| 39 | + | close (ファイル識別子); | |
| 40 | + | ``` | |
| 41 | + | ||
| 42 | + | ここにはファイルを使用したプログラム例を示す。このプログラムは Turbo Pascal と DOS 用に書かれており、file1.txt の最初の文字からなる file2.txt を作り出す。 | |
| 43 | + | ||
| 44 | + | ``` pascal | |
| 45 | + | program CopyOneByteFile; | |
| 46 | + | ||
| 47 | + | var | |
| 48 | + | mychar : char; | |
| 49 | + | filein, fileout : text; | |
| 50 | + | ||
| 51 | + | begin | |
| 52 | + | assign (filein, 'c:\file1.txt'); | |
| 53 | + | reset (filein); | |
| 54 | + | assign (fileout, 'c:\file2.txt'); | |
| 55 | + | rewrite (fileout); | |
| 56 | + | read (filein, mychar); | |
| 57 | + | write (fileout, mychar); | |
| 58 | + | close(filein); | |
| 59 | + | close(fileout) | |
| 60 | + | end. | |
| 61 | + | ``` | |
| 62 | + | ||
| 63 | + | | | | | | |
| 64 | + | |----|----|----| | |
| 65 | + | | [[Basic Pascal Tutorial/Chapter 2/Formatting output/ja|previous]] | [[Basic Pascal Tutorial/Contents/ja|contents]] | [[Basic Pascal Tutorial/Chapter 2/EOLN and EOF/ja|next]] | | |
| 66 | + | ||
| 67 | + | --- | |
| 68 | + | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 2/Files/ja", as it stood on 2022-08-20 after a change by Kai Burghardt, 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).* |
