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

From Lazarus wiki
Jump to navigationJump to search
Line 3: Line 3:
 
2D - ファイル (著者: Tao Yue, 状態: 原文のまま修正なし)
 
2D - ファイル (著者: Tao Yue, 状態: 原文のまま修正なし)
  
Reading from a file instead of the console (keyboard) can be done by:
+
画面(キーボード)からのかわりにファイルから読み込む場合には次のように行う。
 
<syntaxhighlight>
 
<syntaxhighlight>
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>
 
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:
 
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:

Revision as of 09:26, 6 August 2015

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

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

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

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

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

var
  ...
  filein, fileout : text;

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

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:

reset (file_variable, 'filename.extension');
rewrite (file_variable, 'filename.extension');

reset opens a file for reading, and rewrite opens a file for writing. A file opened with reset can only be used with read and readln. A file opened with rewrite can only be used with write and 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