Difference between revisions of "Basic Pascal Tutorial/Chapter 2/Files/ja"
Line 18: | Line 18: | ||
<tt>text</tt> というデータタイプはファイルが単なるテキストであることを示している。 | <tt>text</tt> というデータタイプはファイルが単なるテキストであることを示している。 | ||
− | + | ファイルのための変数を宣言した後、その変数への書き込み、あるいはその変数からの読み込みをする前に、ディスク上のファイル名と関連づけてファイルをオープンしなくてはならない。これには2つのやり方のいずれかを使う。典型的には次のようになる。 | |
− | reset ( | + | reset (ファイル変数, 'ファイル名.拡張子'); |
− | rewrite ( | + | rewrite (ファイル変数, 'ファイル名.拡張子'); |
− | <tt>reset</tt> | + | <tt>reset</tt> は読み込みのためにファイルをオープンし、 <tt>rewrite</tt> は書き込みのためにファイルをオープンする。<tt>reset</tt> でオープンされたファイルには <tt>read</tt> と <tt>readln</tt>だけが使える。 <tt>rewrite</tt> でオープンされたファイルには<tt>write</tt> と <tt>writeln</tt>だけが使える。 |
Turbo Pascal introduced the assign notation. First you assign a filename to a variable, then you call <tt>reset</tt> or <tt>rewrite</tt> using only the variable. | Turbo Pascal introduced the assign notation. First you assign a filename to a variable, then you call <tt>reset</tt> or <tt>rewrite</tt> using only the variable. |
Revision as of 20:33, 7 August 2015
│
български (bg) │
Deutsch (de) │
English (en) │
français (fr) │
日本語 (ja) │
中文(中国大陆) (zh_CN) │
2D - ファイル (著者: Tao Yue, 状態: 原文のまま修正なし)
画面(キーボード)からのかわりにファイルから読み込む場合には次のように行う。
read (ファイル変数, 引数リスト);
write (ファイル変数, 引数リスト);
readln と writeln と同様にファイル変数は以下のように宣言される。
var
...
filein, fileout : text;
text というデータタイプはファイルが単なるテキストであることを示している。
ファイルのための変数を宣言した後、その変数への書き込み、あるいはその変数からの読み込みをする前に、ディスク上のファイル名と関連づけてファイルをオープンしなくてはならない。これには2つのやり方のいずれかを使う。典型的には次のようになる。
reset (ファイル変数, 'ファイル名.拡張子'); rewrite (ファイル変数, 'ファイル名.拡張子');
reset は読み込みのためにファイルをオープンし、 rewrite は書き込みのためにファイルをオープンする。reset でオープンされたファイルには read と readlnだけが使える。 rewrite でオープンされたファイルにはwrite と writelnだけが使える。
Turbo Pascal introduced the assign notation. First you assign a filename to a variable, then you call reset or rewrite using only the variable.
assign (file_variable, 'filename.extension');
reset (file_variable);
The method of representing the path differs depending on your operating system. Windows uses backslashes and drive letters due to its DOS heritage (e.g. c:\directory\name.pas), while MacOS X and Linux use forward slashes due to their UNIX heritage.
After you're done with the file, you can close it with:
close (File_Identifier);
Here's an example of a program that uses files. This program was written for Turbo Pascal and DOS, and will create file2.txt with the first character from file1.txt:
program CopyOneByteFile;
var
mychar : char;
filein, fileout : text;
begin
assign (filein, 'c:\file1.txt');
reset (filein);
assign (fileout, 'c:\file2.txt');
rewrite (fileout);
read (filein, mychar);
write (fileout, mychar);
close(filein);
close(fileout)
end.
previous | contents | next |