Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 檔案.

Similar presentations


Presentation on theme: "Chapter 8 檔案."— Presentation transcript:

1 Chapter 8 檔案

2 8-0 檔案基本概念 檔案讀取方式 循序讀取: 對一個檔案在讀入或者是寫出時,我們只能從頭開始,一步步地向下來一筆一筆地讀取資料。
直接讀取: 對一個檔案在讀入或者是寫出資籿時,我們可以任意、直接地跳躍到檔案的任何一個位置上來從事讀取的工作。

3 8-0 檔案基本概念 檔案儲存模式 文字檔: 把所有的資料都用我們人眼可以明白理解的字元符號來做儲存。優點:易懂,可直接修改。
二進位檔: 直接把資料在電腦記憶中的儲存情形(也就是二進位碼)直接寫入檔案中。優點:讀取較快速、省空間。

4 8-1 The open statement 程式說明 設定參數說明
OPEN(unit = int_expr, file = char_expr, status = char_expr, action = char_expr, iostat = int_var) 設定參數說明 unit = int_expr 開啟一個檔案時要給定這個檔案一個讀取的編號,以後使用 write, read時使用這個編號就可以對這個檔案來讀寫了 int_expr = 數字 → 最好避開1, 2, 5, 6 2, 6是指內定的輸出位置,也就是螢幕 1, 5則是指內定的輸入位置,也就是鍵盤 開啟檔案 開啟檔案的設定參數

5 8-1 The open statement 設定參數說明 file = char_expr status = char_expr
用來指定開啟檔案的名稱。 char_expr = 字串 →包括主檔名與副檔名 Ex. "a.dat" status = char_expr ‘New’, ‘Oid’, ‘Scratch’ or ‘unknown’用來標示是要開啟一個新檔 或是已經存在的舊檔 char_expr = ‘New’ →這個檔案原本不存在,是第一次開啟 ‘Oid’ →這個檔案原本就已經存在

6 8-1 The open statement 設定參數說明 action = char_expr iostate = int_var
‘read’, ‘write’, ‘readwrite’ 表示檔案用來讀取還是寫入 char_expr = ‘readwrite’ → 表示所開啟的檔案可以用來讀取及 寫入,這是內定值 ‘read’ →表示所開啟的檔案只能用來讀取資料 ‘write’ →表示所開啟的檔案只能用來寫入資料 iostate = int_var 表示檔案開啟的狀態,int_var為一個整數變數,lostate會給變數 一個值 Int_var > 0 →表示讀取動作發生錯誤 = 0 →表示讀取動作正常 < 0 →檔案終了

7 8-1 The open statement 設定參數說明 access = char_expr position = char_expr
‘sequential’, ‘direct’設定讀取檔案的方式 char_expr = ‘sequential’ →讀取檔案的動作會以“循序”的方法 來做讀取 ‘direct’ →讀取檔案的動作可以任意指定位置 position = char_expr ‘asis’, ‘rewind’, ‘append’設定檔案開啟時的讀取位置 char_expr = ‘asis’ →表示檔案開啟時的讀取位置,不特別指定 ,這是內定值 ‘rewind’ →表示檔案開啟時的讀取位置移到檔案的 開頭處 ‘append’ →表示檔案開啟時的讀取位置移到檔案 的結尾處

8 8-1 The open statement 範例 opening a file for input
opening a file for output < Ex. 程式片段> integer :: ierror open (unit = 8, file = ‘a.dat', status = 'OLD', action = 'read', iostat = ierror) < Ex. 程式片段> integer ::n_unit, ierror character(len = 7) :: name n_unit = 25 name = 'out.dat' open (unit=n_unit, file=name, status='new', action='write', iostat=ierror)

9 8-1 The open statement 範例 opening a scratch file < Ex. 程式片段>
open (unit = 12, status = 'scratch', iostat= ierror)

10 8-3 reads and writes to disk files
程式說明 read(3, *, iostat = status) value 要輸入檔案的編號 表示檔案開啟的狀態,status為一個整數變數,iostat會給變數一個值 status > 0表示讀取動作發生錯誤 status = 0表示讀取動作正常 status < 0表示檔案終了

11 8-3 reads and writes to disk files
範例 read the values of variables x, y and z from the file “input.dat” write the values of variables x, y and z to the file output.dat < Ex. 程式片段> open(unit = 8, file = 'input.dat', status = 'old', iostat = ierror) read(8, *) x, y, z < Ex. 程式片段> open(unit = 9, file = 'output.dat', status = 'new', iostat = ierror) write(9, 100) x, y, z 100 Format('X =', F10.2, 'Y=', F10.2, 'Z=', F10.2)

12 8-2 The close statement 程式說明 close (close_list) 關閉檔案 要關閉檔案的編號

13 8-3 reads and writes to disk files
< Ex. 完成程式> program read implicit none character (len =20) :: filename integer :: nvals = 0 integer :: status real :: value ! Get the file name and echo it back to the user. write(*,*) 'Please enter input file name:' read(*,*) filename write(*,10) filename 10 format(1x, 'The input file name is : ', A) <接下頁>

14 8-3 reads and writes to disk files
< Ex. 完成程式> ! Open the file and check for errors on open open(3, file = filename, status = 'old', action = 'read', iostat = status) openif: If(status == 0) then ! open was OK. Read values readloop: do read(3, *, iostat = status) value if(status /= 0) Exit nvals = nvals + 1 write(*, 20) nvals, value format(1x, 'Line', I6, ': Value = ', F10.4) end do readloop <接下頁>

15 8-3 reads and writes to disk files
< Ex. 完成程式> ! The while loop has terminated. Was it because of a read error or ! because of the end of the input file? readif: if(status > 0) then write(*, 30) nvals + 1 format(1x, 'An error occurred reading line', I6) else write(*, 40) nvals format(1x, 'End of file reached. There were ', I6, 'values in the file.') end if readif <接下頁>

16 8-3 reads and writes to disk files
< Ex. 完成程式> else openif write(*, 50) status 50 format(1x, 'Error opening file: IOSTAT = ',I6) end if openif close(3) end program read < Ex. 執行結果> 沒有input.dat的檔案 Please enter input file name: input.dat <輸入input.dat [ENTER] > The input file name is : input.dat Error opening file: IOSTAT =

17 8-3 reads and writes to disk files
< Ex. 執行結果> input.dat檔案內容 12 34 56 78 90 < Ex. 執行結果> Please enter input file name: input.dat <輸入input.dat [ENTER] > The input file name is : input.dat Line 1: Value = Line 2: Value = Line 3: Value = Line 4: Value = Line 5: Value = End of file reached. There were values in the file.

18 8-3 reads and writes to disk files
直接存取檔的操作 把檔案的空間、內容事先加以分割成一個個同樣大小的小 區塊,並且把這些區塊按順序加以編號。而讀寫檔案時, 要先指定檔案讀寫位置要在那一個區塊上,才能進行讀寫 的工作。 直接存取檔可以任意在檔案的任何一個地方來進行讀寫的 工作。

19 8-3 reads and writes to disk files
< Ex. 完成程式> “兄弟象”在一場棒球比賽中的打擊者打擊率依棒次順序列表在檔案List中如下: 3.12 2.98 3.34 2.86 2.54 2.78 2.23 2.56 請寫一個可以由棒次來查尋打者打擊率的程式。 <接下頁>

20 8-3 reads and writes to disk files
在DOS作業系統中,文件檔中每一行的行尾都有兩個看不見的符號用來代表一行文字的結束。所以真正一行的長度就是"一行文字字元的數量再加上2"。在List檔中每行長度 = = 6 < Ex. 完成程式> program ex0801 implicit none character(len = 20), parameter :: input = 'List' integer, parameter :: players = 9 integer :: player integer, parameter :: rec_length = 6 real :: hit_rate open(10, file = input,form = 'formatted',access = 'direct',recl = rec_length) do while (.true.) write(*,*) 'Number:' read(*,*) player <接下頁> 在unix中,每一行的行尾只需一個結束符號,所以一行的長度就是"一行文字字元的數量再加1" 開啟直接讀取檔時,open敘述中的access = 'direct'及recl後的數值不能省略。這個數值是用來切分出檔案區塊大小使用的

21 8-3 reads and writes to disk files
< Ex. 完成程式> if(player < 1 .or. player > players) exit read(10, fmt = '(F4.2)', rec = player) hit_rate write(*, 100) 'Number ', player, 'hit_rate = ', hit_rate format(1X, A8, I2, A10, F5.2) end do stop end program ex0801 < Ex. 執行結果> TEST

22 8-3 reads and writes to disk files
< Ex. 完成程式> 依選手的背號順序,輸入選手的打擊率 program ex0802 implicit none character(len = 20), parameter :: input = 'newList' integer, parameter :: players = 9, rec_length = 6 integer :: player real :: hit_rate open(10,file = input,form = 'formatted',access = 'direct', & recl = rec_length) do while (.true.) write(*,*) 'Hit Number:' read(*,*) player <接下頁>

23 8-3 reads and writes to disk files
< Ex. 完成程式> if(player < 1 .or. player > players) exit read(10, fmt = '(F4.2)', rec = player) hit_rate write(*,*) 'Input hit rate:' read(*,*) hit_rate write(10, fmt = '(F4.2)', rec = player) hit_rate end do stop end program ex0802 < Ex. 執行結果> TEST

24 8-3 reads and writes to disk files
二進位檔的操作 二進位檔:肉眼無法明白了解之亂碼檔 < Ex. 完成程式> 把輸入棒球選手打擊率的程式,改成使用二進位檔來運作: program ex0803 implicit none character(len = 20), parameter :: output = 'List.bin' integer, parameter :: players = 9, rec_length = 4 integer :: player real :: hit_rate open(10,file=output,form= 'unformatted',access= 'direct',& recl= rec_length) <接下頁>

25 8-3 reads and writes to disk files
< Ex. 完成程式> do while (.true.) write(*,*) 'Hit Number :' read(*,*) player if (player < 1 .or. player > players) exit read(*,*) hit_rate write(10, rec = player) hit_rate end do stop end program ex0803 < Ex. 執行結果> TEST


Download ppt "Chapter 8 檔案."

Similar presentations


Ads by Google