




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 8 檔案8-0 檔案基本概念檔案讀取方式檔案讀取方式1)循序讀取:循序讀取:對一個檔案在讀入或者是寫出時,我們只能從頭開始,一步步地向下來一筆一筆地讀取資料。2)直接讀取:直接讀取:對一個檔案在讀入或者是寫出資籿時,我們可以任意、直接地跳躍到檔案的任何一個位置上來從事讀取的工作。8-0 檔案基本概念檔案儲存模式檔案儲存模式1)文字檔:文字檔:把所有的資料都用我們人眼可以明白理解的字元符號來做儲存。優點:易懂,可直接修改。2)二進位檔:二進位檔:直接把資料在電腦記憶中的儲存情形(也就是二進位碼)直接寫入檔案中。優點:讀取較快速、省空間。8-1 The open statement
2、程式說明程式說明OPEN(unit = int_expr, file = char_expr, status = char_expr, action = char_expr, iostat = int_var)設定參數說明設定參數說明a)unit = int_expr開啟一個檔案時要給定這個檔案一個讀取的編號,以後使用write, read時使用這個編號就可以對這個檔案來讀寫了int_expr = 數字數字 最好避開1, 2, 5, 6 2, 6是指內定的輸出位置,也就是螢幕 1, 5則是指內定的輸入位置,也就是鍵盤開啟檔案開啟檔案的設定參數8-1 The open statement設定參數
3、說明設定參數說明b)file = char_expr用來指定開啟檔案的名稱。char_expr = 字串字串 包括主檔名與副檔名 Ex. a.datc)status = char_exprNew, Oid, Scratch or unknown用來標示是要開啟一個新檔或是已經存在的舊檔char_expr = New 這個檔案原本不存在,是第一次開啟 Oid 這個檔案原本就已經存在8-1 The open statement設定參數說明設定參數說明d)action = char_exprread, write, readwrite 表示檔案用來讀取還是寫入char_expr = readwrit
4、e 表示所開啟的檔案可以用來讀取及 寫入,這是內定值 read 表示所開啟的檔案只能用來讀取資料 write 表示所開啟的檔案只能用來寫入資料e)iostate = int_var表示檔案開啟的狀態,int_var為一個整數變數,lostate會給變數一個值Int_var 0 表示讀取動作發生錯誤 = 0 表示讀取動作正常 0 檔案終了8-1 The open statement設定參數說明設定參數說明f)access = char_exprsequential, direct設定讀取檔案的方式char_expr = sequential 讀取檔案的動作會以“循序”的方法 來做讀取 direc
5、t 讀取檔案的動作可以任意指定位置g)position = char_exprasis, rewind, append設定檔案開啟時的讀取位置char_expr = asis 表示檔案開啟時的讀取位置,不特別指定 ,這是內定值 rewind 表示檔案開啟時的讀取位置移到檔案的 開頭處 append 表示檔案開啟時的讀取位置移到檔案 的結尾處8-1 The open statement範例範例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範例範例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要輸入檔案的編號表示檔案開啟的狀態,status為一個整數變數,iostat會給變數一個值status 0表示讀取動作發生錯誤status = 0表示讀取動作正常status 0表示檔案終了8-3 reads and writes to disk files範例範例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)關閉檔案要關閉檔案的編號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檔案內容12 34 56 78 90 8-3 reads and writes to disk files直接存取檔的操作直接存取檔的操作把檔案的空間、內容事先加以分割成一個個同樣大小的小把檔案的空間、內容事先加以分割成一個個同樣大小的小區塊,並且把這些區塊按順序加以編號。而讀寫檔案時,區塊,並且把這些區塊按順序加以編號。而讀寫檔案時,要先指定檔案讀寫位置要在那一個區塊上,才能進行讀寫要先指定檔案讀寫位置要在那一個區塊
15、上,才能進行讀寫的工作。的工作。直接存取檔可以任意在檔案的任何一個地方來進行讀寫的直接存取檔可以任意在檔案的任何一個地方來進行讀寫的工作。工作。8-3 reads and writes to disk files“兄弟象”在一場棒球比賽中的打擊者打擊率依棒次順序列表在檔案List中如下:3.122.983.342.862.542.782.232.56請寫一個可以由棒次來查尋打者打擊率的程式。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開啟直接讀取檔時,開啟直接讀取檔時,open敘述中的敘述中的access = direct及及recl後的數值不能省略。後的數值不
17、能省略。這個數值是用來切分出檔這個數值是用來切分出檔案區塊大小使用的案區塊大小使用的在在DOS作業系統中,文件檔中每一行的行尾都有兩個看不見的作業系統中,文件檔中每一行的行尾都有兩個看不見的符號用來代表一行文字的結束。所以真正一行的長度就是符號用來代表一行文字的結束。所以真正一行的長度就是一行一行文字字元的數量再加上文字字元的數量再加上2。在。在List檔中每行長度檔中每行長度 = 4 + 2 = 6在在unix中,每一行的行尾只需一個結中,每一行的行尾只需一個結束符號,所以一行的長度就是束符號,所以一行的長度就是一行文一行文字字元的數量再加字字元的數量再加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依選手的背號順序,輸入選手的打擊率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等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 法院书记员招聘2023年笔试考试必做题有答案
- 办公设备店导购员劳动合同3篇
- 小学生自觉保护环境保证书3篇
- 保全授权的优点3篇
- 地质考察招标文件3篇
- 2024财务出纳述职报告(18篇)
- 代收账款委托书通俗3篇
- 2024年小学英语教师述职报告(25篇)
- 产品授权委托书撰写指南详解3篇
- 就业协议与劳动合同的要点解读
- 小学二年级下口算题1000道(50道每天)
- 香港私家车转让协议书模板
- 食品经营安全管理制度目录
- 浙江省石材面板保温装饰板外墙外保温系统应用技术规程
- 汽车租赁合同协议电子版
- 模拟电子技术基础智慧树知到期末考试答案章节答案2024年北京航空航天大学
- 16J916-1住宅排气道一
- (高清版)JTGT 5640-2020 农村公路养护预算编制办法
- T-CCAA 39-2022碳管理体系 要求
- 人教版语文二年级下册第一二单元百词竞赛
- 幼儿园大班健康《我会保护眼睛》说课课件
评论
0/150
提交评论