Difference between revisions of "Basic Pascal Tutorial/Chapter 2/Files/ja"

From Lazarus wiki
Jump to navigationJump to search
m (bypass language bar/categorization template redirect [cf. discussion])
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Files}}
+
{{Basic Pascal Tutorial/Chapter 2/Files}}
  
 
2D - ファイル (著者: Tao Yue, 状態: 原文のまま修正なし)
 
2D - ファイル (著者: Tao Yue, 状態: 原文のまま修正なし)
  
Reading from a file instead of the console (keyboard) can be done by:
+
画面(キーボード)からのかわりにファイルから読み込む場合には次のように行う。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
read (file_variable, argument_list);
+
read (ファイル変数, 引数リスト);
write (file_variable, argument_list);
+
write (ファイル変数, 引数リスト);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Similarly with <tt>readln</tt> and <tt>writeln</tt>. file_variable is declared as follows:
+
<tt>readln</tt> <tt>writeln</tt> と同様にファイル変数は以下のように宣言される。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
var
 
var
 
   ...
 
   ...
Line 16: Line 16:
 
</syntaxhighlight>  
 
</syntaxhighlight>  
  
The <tt>text</tt> data type indicates that the file is just plain text.
+
<tt>text</tt> というデータタイプはファイルが単なるテキストであることを示している。
  
After declaring a variable for the file, and before reading from or writing to it, we need to associate the variable with the filename on the disk and open the file. This can be done in one of two ways. Typically:
+
ファイルのための変数を宣言した後、その変数への書き込み、あるいはその変数からの読み込みをする前に、ディスク上のファイル名と関連づけてファイルをオープンしなくてはならない。これには2つのやり方のいずれかを使う。典型的には次のようになる。
  reset (file_variable, 'filename.extension');
+
  reset (ファイル変数, 'ファイル名.拡張子');
  rewrite (file_variable, 'filename.extension');
+
  rewrite (ファイル変数, 'ファイル名.拡張子');
<tt>reset</tt> opens a file for reading, and rewrite opens a file for writing. A file opened with <tt>reset</tt> can only be used with <tt>read</tt> and <tt>readln</tt>. A file opened with <tt>rewrite</tt> can only be used with <tt>write</tt> and <tt>writeln</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 は割り当て表記 (assign notation) を導入した。最初にファイル名を変数に割り当てれば、その変数を使うだけで<tt>reset</tt> あるいは <tt>rewrite</tt> を呼び出せるのである。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
assign (file_variable, 'filename.extension');
+
assign (ファイル変数, 'ファイル名.拡張子');
reset (file_variable);
+
reset (ファイル変数);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
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. <tt>c:\directory\name.pas</tt>), while MacOS X and Linux use forward slashes due to their UNIX heritage.
+
パスを表現する方法はオペレーティング・システムによって異なる。 Windows のユーザなら DOS の遺産のためにバック・スラッシュとドライブを表す文字を使うだろう (たとえば、 <tt>c:\directory\name.pas</tt>)。一方、 macOS と Linux では UNIX の遺産で普通のスラッシュ(forward slashes) を使うことになるだろう。
  
After you're done with the file, you can close it with:
+
ファイルで作業をした後には、次のようにしてファイルを閉じることができる。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
close (File_Identifier);
+
close (ファイル識別子);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
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:
+
ここにはファイルを使用したプログラム例を示す。このプログラムは Turbo Pascal DOS 用に書かれており、file1.txt の最初の文字からなる file2.txt を作り出す。
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
program CopyOneByteFile;
 
program CopyOneByteFile;
  
Line 57: Line 57:
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Formatting_output/ja|previous]]   
+
|[[Basic Pascal Tutorial/Chapter 2/Formatting output/ja|previous]]   
|[[Contents/ja|contents]]  
+
|[[Basic Pascal Tutorial/Contents/ja|contents]]  
|[[EOLN_and_EOF/ja|next]]
+
|[[Basic Pascal Tutorial/Chapter 2/EOLN and EOF/ja|next]]
 
|}
 
|}

Latest revision as of 15:18, 20 August 2022

български (bg) Deutsch (de) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

2D - ファイル (著者: Tao Yue, 状態: 原文のまま修正なし)

画面(キーボード)からのかわりにファイルから読み込む場合には次のように行う。

read (ファイル変数, 引数リスト);
write (ファイル変数, 引数リスト);

readlnwriteln と同様にファイル変数は以下のように宣言される。

var
  ...
  filein, fileout : text;

text というデータタイプはファイルが単なるテキストであることを示している。

ファイルのための変数を宣言した後、その変数への書き込み、あるいはその変数からの読み込みをする前に、ディスク上のファイル名と関連づけてファイルをオープンしなくてはならない。これには2つのやり方のいずれかを使う。典型的には次のようになる。

reset (ファイル変数, 'ファイル名.拡張子');
rewrite (ファイル変数, 'ファイル名.拡張子');

reset は読み込みのためにファイルをオープンし、 rewrite は書き込みのためにファイルをオープンする。reset でオープンされたファイルには readreadlnだけが使える。 rewrite でオープンされたファイルにはwritewritelnだけが使える。

Turbo Pascal は割り当て表記 (assign notation) を導入した。最初にファイル名を変数に割り当てれば、その変数を使うだけでreset あるいは rewrite を呼び出せるのである。

assign (ファイル変数, 'ファイル名.拡張子');
reset (ファイル変数);

パスを表現する方法はオペレーティング・システムによって異なる。 Windows のユーザなら DOS の遺産のためにバック・スラッシュとドライブを表す文字を使うだろう (たとえば、 c:\directory\name.pas)。一方、 macOS と Linux では UNIX の遺産で普通のスラッシュ(forward slashes) を使うことになるだろう。

ファイルで作業をした後には、次のようにしてファイルを閉じることができる。

close (ファイル識別子);

ここにはファイルを使用したプログラム例を示す。このプログラムは Turbo Pascal と DOS 用に書かれており、file1.txt の最初の文字からなる file2.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