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 提供了許多方法,它可以幫助處理檔的列表。


上一篇: Euphoria 函數 下一篇:無