




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Chapter 8 檔案8-0 檔案基本概念檔案讀取方式檔案讀取方式1)循序讀?。貉蜃x?。簩?duì)一個(gè)檔案在讀入或者是寫出時(shí),我們只能從頭開始,一步步地向下來一筆一筆地讀取資料。2)直接讀?。褐苯幼x?。簩?duì)一個(gè)檔案在讀入或者是寫出資籿時(shí),我們可以任意、直接地跳躍到檔案的任何一個(gè)位置上來從事讀取的工作。8-0 檔案基本概念檔案儲(chǔ)存模式檔案儲(chǔ)存模式1)文字檔:文字檔:把所有的資料都用我們?nèi)搜劭梢悦靼桌斫獾淖衷?hào)來做儲(chǔ)存。優(yōu)點(diǎn):易懂,可直接修改。2)二進(jìn)位檔:二進(jìn)位檔:直接把資料在電腦記憶中的儲(chǔ)存情形(也就是二進(jìn)位碼)直接寫入檔案中。優(yōu)點(diǎn):讀取較快速、省空間。8-1 The open statement
2、程式說明程式說明OPEN(unit = int_expr, file = char_expr, status = char_expr, action = char_expr, iostat = int_var)設(shè)定參數(shù)說明設(shè)定參數(shù)說明a)unit = int_expr開啟一個(gè)檔案時(shí)要給定這個(gè)檔案一個(gè)讀取的編號(hào),以後使用write, read時(shí)使用這個(gè)編號(hào)就可以對(duì)這個(gè)檔案來讀寫了int_expr = 數(shù)字?jǐn)?shù)字 最好避開1, 2, 5, 6 2, 6是指內(nèi)定的輸出位置,也就是螢?zāi)?1, 5則是指內(nèi)定的輸入位置,也就是鍵盤開啟檔案開啟檔案的設(shè)定參數(shù)8-1 The open statement設(shè)定參數(shù)
3、說明設(shè)定參數(shù)說明b)file = char_expr用來指定開啟檔案的名稱。char_expr = 字串字串 包括主檔名與副檔名 Ex. a.datc)status = char_exprNew, Oid, Scratch or unknown用來標(biāo)示是要開啟一個(gè)新檔或是已經(jīng)存在的舊檔char_expr = New 這個(gè)檔案原本不存在,是第一次開啟 Oid 這個(gè)檔案原本就已經(jīng)存在8-1 The open statement設(shè)定參數(shù)說明設(shè)定參數(shù)說明d)action = char_exprread, write, readwrite 表示檔案用來讀取還是寫入char_expr = readwrit
4、e 表示所開啟的檔案可以用來讀取及 寫入,這是內(nèi)定值 read 表示所開啟的檔案只能用來讀取資料 write 表示所開啟的檔案只能用來寫入資料e)iostate = int_var表示檔案開啟的狀態(tài),int_var為一個(gè)整數(shù)變數(shù),lostate會(huì)給變數(shù)一個(gè)值Int_var 0 表示讀取動(dòng)作發(fā)生錯(cuò)誤 = 0 表示讀取動(dòng)作正常 0 檔案終了8-1 The open statement設(shè)定參數(shù)說明設(shè)定參數(shù)說明f)access = char_exprsequential, direct設(shè)定讀取檔案的方式char_expr = sequential 讀取檔案的動(dòng)作會(huì)以“循序”的方法 來做讀取 direc
5、t 讀取檔案的動(dòng)作可以任意指定位置g)position = char_exprasis, rewind, append設(shè)定檔案開啟時(shí)的讀取位置char_expr = asis 表示檔案開啟時(shí)的讀取位置,不特別指定 ,這是內(nèi)定值 rewind 表示檔案開啟時(shí)的讀取位置移到檔案的 開頭處 append 表示檔案開啟時(shí)的讀取位置移到檔案 的結(jié)尾處8-1 The open statement範(fàn)例範(fàn)例a)opening a file for inputb)opening a file for outputinteger : ierroropen (unit = 8, file = a.dat, stat
6、us = OLD, action = read, iostat = ierror)integer :n_unit, ierrorcharacter(len = 7) : namen_unit = 25name = out.datopen (unit=n_unit, file=name, status=new, action=write, iostat=ierror)8-1 The open statement範(fàn)例範(fàn)例c)opening a scratch fileopen (unit = 12, status = scratch, iostat= ierror)8-3 reads and wr
7、ites to disk files程式說明程式說明read(3, *, iostat = status) value要輸入檔案的編號(hào)表示檔案開啟的狀態(tài),status為一個(gè)整數(shù)變數(shù),iostat會(huì)給變數(shù)一個(gè)值status 0表示讀取動(dòng)作發(fā)生錯(cuò)誤status = 0表示讀取動(dòng)作正常status 0表示檔案終了8-3 reads and writes to disk files範(fàn)例範(fàn)例1)read the values of variables x, y and z from the file “input.dat”2)write the values of variables x, y and
8、z to the file output.datopen(unit = 8, file = input.dat, status = old, iostat = ierror)read(8, *) x, y, z open(unit = 9, file = output.dat, status = new, iostat = ierror) write(9, 100) x, y, z100 Format(X =, F10.2, Y=, F10.2, Z=, F10.2)8-2 The close statement程式說明程式說明close (close_list)關(guān)閉檔案要關(guān)閉檔案的編號(hào)8-3
9、 reads and writes to disk filesprogram 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) filename10 format(1x, The input file na
10、me is : , A)8-3 reads and writes to disk files ! 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
11、+ 1 write(*, 20) nvals, value20 format(1x, Line, I6, : Value = , F10.4) end do readloop8-3 reads and writes to disk files ! 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 + 130 format(1x, An error
12、occurred reading line, I6) else write(*, 40) nvals40 format(1x, End of file reached. There were , I6, values in the file.) end if readif8-3 reads and writes to disk files else openif write(*, 50) status 50 format(1x, Error opening file: IOSTAT = ,I6) end if openif close(3)end program read沒有input.dat
13、的檔案Please enter input file name:input.dat The input file name is : input.datError opening file: IOSTAT = 28-3 reads and writes to disk filesPlease enter input file name:input.dat The input file name is : input.datLine 1: Value = 12.0000Line 2: Value = 34.0000Line 3: Value = 56.0000Line 4: Value = 78
14、.0000Line 5: Value = 90.0000End of file reached. There were 5values in the file.input.dat檔案內(nèi)容12 34 56 78 90 8-3 reads and writes to disk files直接存取檔的操作直接存取檔的操作把檔案的空間、內(nèi)容事先加以分割成一個(gè)個(gè)同樣大小的小把檔案的空間、內(nèi)容事先加以分割成一個(gè)個(gè)同樣大小的小區(qū)塊,並且把這些區(qū)塊按順序加以編號(hào)。而讀寫檔案時(shí),區(qū)塊,並且把這些區(qū)塊按順序加以編號(hào)。而讀寫檔案時(shí),要先指定檔案讀寫位置要在那一個(gè)區(qū)塊上,才能進(jìn)行讀寫要先指定檔案讀寫位置要在那一個(gè)區(qū)塊
15、上,才能進(jìn)行讀寫的工作。的工作。直接存取檔可以任意在檔案的任何一個(gè)地方來進(jìn)行讀寫的直接存取檔可以任意在檔案的任何一個(gè)地方來進(jìn)行讀寫的工作。工作。8-3 reads and writes to disk files“兄弟象”在一場(chǎng)棒球比賽中的打擊者打擊率依棒次順序列表在檔案List中如下:3.122.983.342.862.542.782.232.56請(qǐng)寫一個(gè)可以由棒次來查尋打者打擊率的程式。8-3 reads and writes to disk filesprogram ex0801 implicit none character(len = 20), parameter : input =
16、 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開啟直接讀取檔時(shí),開啟直接讀取檔時(shí),open敘述中的敘述中的access = direct及及recl後的數(shù)值不能省略。後的數(shù)值不
17、能省略。這個(gè)數(shù)值是用來切分出檔這個(gè)數(shù)值是用來切分出檔案區(qū)塊大小使用的案區(qū)塊大小使用的在在DOS作業(yè)系統(tǒng)中,文件檔中每一行的行尾都有兩個(gè)看不見的作業(yè)系統(tǒng)中,文件檔中每一行的行尾都有兩個(gè)看不見的符號(hào)用來代表一行文字的結(jié)束。所以真正一行的長度就是符號(hào)用來代表一行文字的結(jié)束。所以真正一行的長度就是一行一行文字字元的數(shù)量再加上文字字元的數(shù)量再加上2。在。在List檔中每行長度檔中每行長度 = 4 + 2 = 6在在unix中,每一行的行尾只需一個(gè)結(jié)中,每一行的行尾只需一個(gè)結(jié)束符號(hào),所以一行的長度就是束符號(hào),所以一行的長度就是一行文一行文字字元的數(shù)量再加字字元的數(shù)量再加18-3 reads and wri
18、tes to disk files if(player players) exit read(10, fmt = (F4.2), rec = player) hit_rate write(*, 100) Number , player, hit_rate = , hit_rate100 format(1X, A8, I2, A10, F5.2) end do stopend program ex0801 TEST8-3 reads and writes to disk files依選手的背號(hào)順序,輸入選手的打擊率program ex0802 implicit none character(le
19、n = 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(*,*) player8-3 reads and writes to disk files if(player players) exit read(10, fmt = (F4.2), rec = player) hit_rate write(*,*) Input hit rate: read(*,*
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 代寫小說合同范例
- 業(yè)務(wù)勞務(wù)合同范例
- 農(nóng)場(chǎng)定制招商加盟合同范例
- 公司聘司機(jī)合同范例
- 農(nóng)業(yè)采購種子合同范例
- 企業(yè)常用商務(wù)合同范例
- 創(chuàng)新存儲(chǔ)設(shè)備采購合同范例
- 公司牛油果批發(fā)合同范例
- 個(gè)人購買鋪面合同范例
- 2025年內(nèi)蒙古機(jī)電職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫及答案一套
- (高清版)TDT 1068-2022 國土空間生態(tài)保護(hù)修復(fù)工程實(shí)施方案編制規(guī)程
- 國家開放大學(xué)《心理與健康》形考任務(wù)1-3參考答案
- 新概念英語第二冊(cè)知識(shí)點(diǎn)梳理
- 中外戲劇史第五章文藝復(fù)興到19世紀(jì)的歐洲戲劇課件
- 臨時(shí)用電報(bào)審表及臨時(shí)用電驗(yàn)收記錄
- 防波堤施工方案
- 華北理工大學(xué)中藥學(xué)教案(64學(xué)時(shí)-田春雨)
- 2022年漢字聽寫大會(huì)競(jìng)賽題庫(含答案)
- 攝影培訓(xùn)教學(xué)課件:攝影用光
- 大學(xué)物理相對(duì)運(yùn)動(dòng)課件
評(píng)論
0/150
提交評(píng)論