BasicsTutorial/File handling

Last-modified: 2008-03-14 (金) 14:39:13

File handling

ファイル処理

You probably know pretty much what files are, you use them when playing games, when using paint programs, you are using one now (unless you printed this off)!
あなたは多分、ファイルが何であるかについて、ほとんどわかっているだろう、ゲームをするとき、あなたはそれらを使う。そのとき、ペンキ・プログラムを用いて、あなたは現在それを使っている(あなたが離れてこれをプリントしなかった限り)!
Files store information, just like variables, but unlike variables they store information even after the program has ended.
ファイルは情報を格納する、(まるで変数のような)しかし、プログラムが終わった後でさえ、変数とは異なり、それは情報を格納する。
This makes them useful for storing things like program settings, documents and high scores.
これは、それらをプログラム・セッティング、文書とハイスコアのようなものを保存することに役立つようにする。

First we will deal with writing a file, then we will load it back into the program.
最初に、我々はファイルを書くことに対処する、そして、我々はそれをプログラムにロードする。

gosub writeData
print "Data Written"
wait key
end
writeData:
     if file exist("myInfo.inf") = 1 then delete file "myInfo.inf"
     open to write 1,"myInfo.inf"
     write string 1,"Alfonse" : `Name
     write byte 1,87 : `Age
     write float 1,1.45 : `Height
     write string 1,"01936111999" : `Telephone
     close file 1
return

The first line of the subroutine checks if there is a file called “myInfo.inf” in the current directory.
サブルーチンの最初の行は、カレント・ディレクトリの「myInfo.inf」と呼ばれているファイルがあるかどうか調べる。
The default directory is the folder that the exe is in, unless it has been run by another program in which case the creator of that exe will decide.
そのexeの作者がどのケースで決めるかはもう一つのプログラムに通されなかった限り、デフォルト・ディレクトリはexeがあるフォルダである。
If this file does exist then we delete it, this is because we want to create a completely new file in its place.
このファイルが存在するならば、我々はそれを削除する、これは我々がその位置で完全に新しいファイルを作成したいからである。
If we did not delete it the information we are about to write would just be shoved on the end of the file, making it longer and longer each time we run the program.
我々がそれを削除しないならば、我々が書こうとしているという情報はちょうどファイルの端で押されるだろう。そして、我々がプログラムを実行するたびに、それをより長くてより長くする。

The second line is where we open the file.
第2の行は、我々がファイルを開くところである。
If the file doesn’t exist it is created using this command.
ファイルが存在しないならば、それはこのコマンドを使用してつくられる。
We open it to write as we want to write data to it as opposed to reading the data as we will later.
我々が後でそうして我々がデータを読むことと対照的にそれにデータを書きたくて、我々は書くためにそれを開ける。
We have to open it and load it into a file slot, 1 in this case.
我々はそれを開けなければならなくて、それをファイル・スロット(この場合は1)に詰めなければならない。

The next four lines write some data to the file.
次の4本の行は、ファイルに若干のデータを書く。
Files lists of data, every time you write to them you add one bit of information to the end of the list.
データ(あなたがそれらにあなたがリストの終わりまで1ビットの情報を加えると書くというあらゆる時)のファイル・リスト。
Here we are writing a string, a byte, a float and a string.
文字、1バイト、フロートと文字を書いてさあどうぞ。
Look in the help files for a full list of data types.
データ型の全リストのために、ヘルプ・ファイルをのぞきなさい。
The commands require the file number to write to and the data to write to it.
命令は、書くファイル番号とそれに書くデータを必要とする。
The colon is there so that I can put multiple commands on a line, in this case a remark.
私が複数の命令を境界(この場合はコメント)に置くことができるように、コロンはそこにある。

We then close the file as we need it no longer and write a confirmation that the data is written.
我々がそれをもはや必要としなくて、データが書かれる確証を書いて、我々はそれからファイルを閉じる。

Now that we have our data written we can read it back in and display it on the screen.
我々が我々のデータを書いておく今、我々は後ろにそれを読み取ることができて、それをスクリーンに表示することができる。

name as string
age as integer
height as float
telephone as string
gosub writeData
print "Data Written"
gosub readData
print "Data Read"
print
gosub printData
wait key
end
writeData:
     if file exist("myInfo.inf") = 1 then delete file "myInfo.inf"
     open to write 1,"myInfo.inf"
     write string 1,"Alfonse" : `Name
     write byte 1,87 : `Age
     write float 1,1.45 : `Height
     write string 1,"01936111999" : `Telephone
     close file 1
return
readData:
     if file exist("myInfo.inf") = 0
           print "File doesn't exist!"
     else
           open to read 1,"myInfo.inf"
           read string 1,name
           read byte 1,age
           read float 1,height
           read string 1,telephone
           close file 1
     endif
return
printData:
     print "Name: ",name
     print "Age: ", age
     print "Height: ", height
     print "Telephone: ", telephone
return

Now we have defined our variables and written the data.
今は、我々は我々の変数を定めて、データを書いた。
Now we open the file again using OPEN TO READ.
現在、我々は読むために、 OPEN TO READ を使う。
This time we can read the data from the file into variables.
今度は、我々は変数にファイルからデータを読み込むことができる。
We use commands like READ STRING and READ BYTE, which require the file number and a variable to store the values in.
我々は READ STRING と READ BYTEのようなコマンドを使用する。そして、それは ファイル番号と格納する変数の値を要求する。
The values are read out in the order they were read in.
値は、それが読み取られたという順番で読み出される。
After we have read the values out we simply print them on to the screen.
我々が値を読み出したあと、我々は単にスクリーンにそれらをプリントする。

There is another command SKIP BYTES fileNumber, bytesToSkip which allows you to skip over values without having to read them into variables.
もう一つの命令 SKIP BYTES fileNumber, bytesToSkip (変数にそれらを読まなければならないことなく値を飛ばすことができる)が、ある。

Summary of Commands

命令の概要

· FILE EXIST(fileNum) - Returns a 1 if a file exists, 0 if it doesn’t.
ファイルが存在する(fileNum)-ファイルが存在するならば、1を返す。(それがそうしないならば0)

· DELETE FILE fileNum - Deletes a file.
ファイルfileNumを削除しなさい-ファイルを削除する。

· OPEN TO WRITE fileNum,filePath$ - Opens a file to write to it, and creates it if it doesn’t exist.
fileNum,filePath$を書くために開いた - それに書くファイルを開けて、そして、それが存在しないならば、それをつくる。

· OPEN TO READ fileNum,filePath$ - Opens a file to read from.
fileNum,filePath$を読むために、開きなさい - 読めるファイルを開く。

· WRITE datatype fileNum,variable - Writes a value to a file.
データ型にfileNum,variableを書きなさいと、ファイルへの値が書く。

· CLOSE FILE fileNum - Closes a file.
クローズファイルfileNum -ファイルを閉じる。

· READ datatype fileNum,variable - Reads a value from a file to a variable.
READデータ型fileNum,variable は、 ファイルから変数への値が読む。

End of Section Tasks

セクション終わりの作業

l Create a program that allows users to store information in a file and read it out later even when they have closed the program.
それがプログラムを閉じたときでも、ユーザーが情報をファイルに格納して、後でそれを音読することができるプログラムを作成しなさい。

l Create two programs that run at the same time, one to write to a file and the other to read the file.
同時に、動作する2つのプログラム(ファイルを読むためにファイルと他に書く1)を作成しなさい。
You could make it save the positions, sizes and colours of shapes on the screen, or do something else altogether.
あなたはそれをスクリーンの上に形の位置、サイズと色をセーブするようにすることができたか、全く何か他のものをする。
You decide.
あなたは決める。