標準入出力関数
openfile(filename, mode)
この関数はmodeで指定されたモードでファイルを開きます。新しいfile_handleを返し、エラーの場合はnilとエラーを説明する文字列を返します。この関数は、定義済みのFVX環境変数_INPUTまたは_OUTPUTを変更しません。modeは次のいずれかになります。
"r“ - read mode;
"w“ - write mode;
"a“ - append mode;
"r+“ - update mode, all previous data is preserved;
"w+“ - update mode, all previous data is erased;
"a+“ - append update mode, previous data is preserved, writing is only allowed at the end of file.
文字列modeは標準的なC言語の関数fopen()で使用されるものとまったく同じです。注: Microsoft Windowsプラットフォームにおいてバイナリモードでファイルを開くには、この文字列の最後に「b」をつける必要があります。
例:
--open a new file "test.txt" in write mode
file_handle = openfile("test.txt", "w")
--export a coordinate surface and read the data from the file
fv_script("EXPORT COORD coord_surface")
data_handle = openfile("coord_surface", "r")
closefile(handle)
この関数は指定されたファイルを閉じます。_INPUTまたは_OUTPUTは変更されません。実行後、ファイルハンドルは無効になります。
例:
--close the file "test.txt"
closefile(file_handle)
readfrom([filename])
この関数は2つの方法で呼び出すことができます。 ファイル名で呼び出した場合、名前付きファイルを開き、そのハンドルを_INPUTの値として設定し、この値を返します。現在の入力ファイルは閉じません。引数なしで呼び出されると、現在の_INPUTファイルを閉じて、stdinが_INPUTの値として復元します。 エラーの場合、nilとエラーを説明する文字列が返されます。
filenameが|で始まる場合、関数popen()によってパイプ入力がオープンされます。 すべてのシステムがパイプを実装するとは限りません。 さらに、同時に開くことができるファイルの数は通常制限され、システムによって異なります。
例:
--set file_handle as the default handle _INPUT
file_handle = readfrom("test.txt")
--close the current _INPUT file and set stdin as the new _INPUT
readfrom()
remove(filename)
指定された名前のファイルを削除します。 エラーの場合、nilとエラーを説明する文字列が返されます。
例:
--delete the file "test.txt"
remove("test.txt")
rename(name1, name2)
name1という名前のファイルをname2という名前に変更します。エラーの場合、nilとエラーを説明する文字列が返されます。
例:
--rename the file "test1.txt" to "test2.txt"
rename("test1.txt", "test2.txt")
read ([filehandle,] format1, ...)
何を読み込むかを指定する書式に従い、_INPUTまたは引数filehandleで指定されたファイルを読み込みます。各書式について、関数は読み込んだ文字列(または数値)を返します。指定された書式でデータを読み込むことができない場合はnilを返します。 書式なしで呼ばれた場合、次の行を読み込むデフォルトの書式が使用されます(下記参照)。
利用可能な書式は以下の通りです。
"* n" - 数値を読み込みます。 これは文字列の代わりに数値を返す唯一の書式です。
"* l" - 次の行(行末はスキップする)を読み込み、ファイルの最後にはnilを読み込みます。 これがデフォルトの形式です。
"* a" - ファイル全体を現在の位置から読み込みます。 ファイルの終わりでは、空の文字列を返します。
"* w" - 次の単語(空白以外の文字列シーケンス)を読み込み、必要であればスペースをスキップし、ファイルの最後にはnilを読み込みます。
"number" - その文字数までの文字列を読み込み、ファイルの最後にはnilを読み込みます。
例:
--read from current open file (from handle _INPUT) one line
--for each time read() is called
read("*l")
--read the entire contents of file "test3.txt" (with handle
--file_handle) with one call to function read()
read(file_handle, "*a")
例:
--export a coordinate surface and read the data from the file
fv_script("EXPORT COORD coord_surface")
data_handle = openfile("coord_surface", "r")
x[i] = read(data_handle, “*n”) -- reads number from first column
y[i] = read(data_handle, "*n") -- reads number from second column
z[i] = read(data_handle, "*n")
s[i] = read(data_handle, "*n") -- reads the scalar value
line = read(data_handle, "*l") -- goes to the next line
write ([filehandle, ] value1, ...)
各引数の値を_OUTPUTまたは引数filehandleで指定されたファイルに書き込みます。 引数は文字列または数値でなければなりません。 他の型の値を書き込むには、writeの前にtostringまたはformatを使用します。 文字列 "¥r"(復帰)または "\n"(改行)は、行の終了のために使用します。MS-Windowsスタイルの行の終了には両方が必要です。エラーの場合、nilとエラーを説明する文字列が返されます。
例:
-- write arguments to current file handle
x = "Hello"
y = ", "
z = "World"
write(x, y, z)
-- write arguments to file "test5.txt" with handle
-- file_handle_5
write(file_handle_5, x, y, z)
writeto([filename])
この関数は2つの方法で呼び出すことができます。ファイル名で呼び出されると、名前付きファイルを開き、そのハンドルを_OUTPUTの値として設定し、この値を返します。現在の出力ファイルは閉じません。ファイルがすでに存在する場合は、この操作により完全に消去されます。引数無しで呼び出すと、この関数は_OUTPUTファイルを閉じ、stdoutを_OUTPUTの値として復元します。エラーの場合、nilとエラーを説明する文字列が返されます。
filenameが|で始まる場合、関数popen()によってパイプ入力がオープンされます。すべてのシステムがパイプを実装するとは限りません。さらに、同時に開くことができるファイルの数は通常制限され、システムによって異なります。
例:
--open the file "test.txt" and return its handle.
--test_handle becomes the same as _OUTPUT.
test_handle = writeto("test.txt")
appendto(filename)
filenameという名前のファイルを開き、それを_OUTPUTの値として設定します。writeto()とは異なり、この関数は以前のファイルの内容を消去しません。代わりにファイルに書き込まれたものがその最後に追加されます。エラーの場合、nilとエラーを説明する文字列が返されます。
例:
--open "test.txt" and set its handle to _OUTPUT.
--Preserves previous content if this file already exists
appendto("test.txt")
|