Euphoria 檔案I/O


使用Euphoria程式設計語言,你可以寫程式讀取和改變你的軟碟驅動器或硬碟驅動器上的檔案資料,或者建立新的檔案形式輸出。你甚至可以存取計算機上的裝置,如列印機和調變解調器。

本章將覆蓋所有 Euphoria 的基本I/O功能。如需使用更多的功能,請的參考標準的 Euphoria 文件。

列印到螢幕上:

產生輸出最簡單的方法是使用puts() 語句,在這裡您可以通過任何螢幕上要顯示的字串。還有另一種方法 printf() 的情況下,也可用於使用動態值來格式化一個字串。

這些方法可以把你把它們傳遞給一個字串,並寫入到標準輸出的結果如下表示式:

#!/home/euphoria-4.0b2/bin/eui
 
puts(1, "Euphoria is really a great language, isn't it?" )

標準螢幕上,這將產生以下結果:

Euphoria is really a great language, isn't it?

開啟和關閉檔案:

Euphoria 提供必要的基本方法,預設情況下對檔案進行操作。你可以做你大多數的檔案操作,使用 open(), close(), printf(), gets() and getc() methods.

open 方法:

讀取或寫入檔案之前,必須開啟它使用欣快內建的open() 方法。這個函式建立一個檔案描述符,這將被用來呼叫與它相關聯的其他支援方式。

語法:

integer file_num = open(file_name, access_mode)

以上方法返回-1的情況下有一個錯誤開啟給定的檔案名。下面是詳細的引數研究:

  • file_name:  file_name 引數是一個字串值,其中包含要存取的檔案的名稱。

  • access_mode: access_mode決定模式,即開啟檔案。讀,寫追加等可能值的完整列表在下面的表中給出。

下面是一個清單的不同的模式開啟一個檔案:

模式 描述
r Opens a text file for reading only. The file pointer is placed at the beginning of the file.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file.
w Opens a text file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
u Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
ub Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

例子:

下面的例子在你的Linux建立一個新的文字檔案在當前目錄:

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile,txt", "w")
if file_num = -1 then
    puts(ERROR, "couldn't open myfile\n")
else
     puts(STDOUT, "File opend successfully\n")
end if

如果檔案成功開啟,那麼它會在當前目錄下建立“myfile.txt”,並會產生以下結果:

File opend successfully

close() 方法:

close() 方法重新整理不成文的任何資訊,並關閉該檔案後,沒有更多的讀取或寫入檔案可以做。

幸福感會自動關閉一個檔案一個檔案時的參考物件被重新分配到另一個檔案。這是一個很好的做法,使用close()方法關閉檔案。

語法:

close( file_num );

這裡傳遞的引數是開啟一個檔案時收到的檔案描述符。

例子:

以下的例子將建立一個檔案上面,然後將現有的程式之前關閉它:

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "w")
if file_num = -1 then
    puts(ERROR, "couldn't open myfile\n")
else
     puts(STDOUT, "File opend successfully\n")
end if

if file_num = -1 then
    puts(ERROR, "No need to close the file\n")
else
     close( file_num )
     puts(STDOUT, "File closed successfully\n")
end if

這將產生以下結果:

File opend successfully
File closed successfully

讀取和寫入檔案:

Euphoria 提供了一套存取方法,使我們的生活更輕鬆,同時讀取或寫入一個檔案,無論是在文字模式或二進位制模式。我們將看到如何使用printf() 和 gets()方法來讀取和寫入檔案。 

printf() 方法:

printf()的方法寫入一個開啟的檔案的任何字串。

語法:

printf(fn, st, x) 

下面是詳細引數:

  • fn: 檔案描述符收到open()方法。

  • st: 十進位制或原子將被格式化%d和字串或字元序列的格式字串將被格式化,用%s。

  • x: 如果x是一個序列,然後從st再配上相應元素的x格式說明符。如果x是一個原子,那麼通常st將只包含一個格式說明符,它將被應用於x,但是如果st包含多種格式說明符,每一個都將被應用到相同的值x。

例子:

以下範例將開啟一個檔案,將寫在這個檔案中,一個人的姓名和年齡:

#!/home/euphoria-4.0b2/bin/eui

integer file_num
constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "w")
if file_num = -1 then
    puts(ERROR, "couldn't open myfile\n")
else
     puts(STDOUT, "File opend successfully\n")
end if

printf(file_num, "My name is %s and age is %d\n", {"Zara", 8})

if file_num = -1 then
    puts(ERROR, "No need to close the file\n")
else
     close( file_num )
     puts(STDOUT, "File closed successfully\n")
end if

上述方法範例建立 myfile.txt 檔案,會寫在該檔案中的內容,並最終將關閉該檔案。如果你想開啟這個檔案,它有以下內容

My name is Zara and age is 8

gets() 方法:

gets() 方法從一個開啟的檔案中讀取字串。

語法:

gets(file_num)

這裡傳遞的引數檔案說明 opend() 方法返回。這種方法的檔案一行行從一開始就開始讀。字元將具有從0到255的值。原子檔案結束時返回-1。

例子:

讓我們採取上面我們已經建立了一個檔案myfile.txt。

#!/home/euphoria-4.0b2/bin/eui

integer file_num
object line

constant ERROR = 2
constant STDOUT = 1

file_num = open("myfile.txt", "r")
if file_num = -1 then
    puts(ERROR, "couldn't open myfile\n")
else
     puts(STDOUT, "File opend successfully\n")
end if

line = gets(file_num)

printf( STDOUT, "Read content : %s\n", {line})

if file_num = -1 then
    puts(ERROR, "No need to close the file\n")
else
     close( file_num )
     puts(STDOUT, "File closed successfully\n")
end if

這將產生以下結果:

File opend successfully
Read content : My name is Zara and age is 8

File closed successfully

檔案和目錄的方法:

Euphoria 提供了許多方法,它可以幫助處理檔案的列表。