
- Euphoria 教程
- Euphoria - 首頁
- Euphoria - 概述
- Euphoria - 環境
- Euphoria - 基本語法
- Euphoria - 變數
- Euphoria - 常量
- Euphoria - 資料型別
- Euphoria - 運算子
- Euphoria - 分支
- Euphoria - 迴圈型別
- Euphoria - 流程控制
- Euphoria - 短路
- Euphoria - 序列
- Euphoria - 日期和時間
- Euphoria - 過程
- Euphoria - 函式
- Euphoria - 檔案 I/O
- Euphoria 有用資源
- Euphoria - 快速指南
- Euphoria - 庫例程
- Euphoria - 有用資源
- Euphoria - 討論
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()
- getc()
open 方法
在讀取或寫入檔案之前,您必須使用 Euphoria 的內建 open() 方法開啟它。此函式建立一個檔案描述符,用於呼叫與其關聯的其他支援方法。
語法
integer file_num = open(file_name, access_mode)
如果開啟給定檔名時出錯,則上述方法返回 -1。以下是引數:
file_name - file_name 引數是一個字串值,包含要訪問的檔案的名稱。
access_mode - access_mode 確定開啟檔案的模式。例如,讀取、寫入追加等。下表列出了檔案開啟模式的所有可能值:
序號 | 模式和描述 |
---|---|
1 | r 以只讀方式開啟文字檔案。檔案指標放置在檔案開頭。 |
2 | rb 以二進位制格式只讀方式開啟檔案。檔案指標放置在檔案開頭。 |
3 | w 以只寫方式開啟文字檔案。如果檔案存在,則覆蓋檔案。如果檔案不存在,則建立一個新檔案以進行寫入。 |
4 | wb 以二進位制格式只寫方式開啟檔案。如果檔案存在,則覆蓋檔案。如果檔案不存在,則建立一個新檔案以進行寫入。 |
5 | u 開啟一個檔案進行讀寫。檔案指標設定在檔案開頭。 |
6 | ub 以二進位制格式開啟一個檔案進行讀寫。檔案指標放置在檔案開頭。 |
7 | a 開啟一個檔案進行追加。如果檔案存在,則檔案指標位於檔案末尾(追加模式)。如果檔案不存在,則建立一個新檔案以進行寫入。 |
8 | ab 以二進位制格式開啟一個檔案進行追加。如果檔案存在,則檔案指標位於檔案末尾(追加模式)。如果檔案不存在,則建立一個新檔案以進行寫入。 |
示例
以下示例在 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() 方法重新整理任何未寫入的資訊並關閉檔案,此後無法再對檔案進行讀取或寫入。
當檔案的引用物件重新分配給另一個檔案時,Euphoria 會自動關閉該檔案。最好使用 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 庫例程 中。