1 / 11

第 15 章 檔案存取(二)

第 15 章 檔案存取(二). Ren-Jie Wang, 王 仁 傑 , Ph.D. rjwang@ntit.edu.tw 課程網頁 : http://home.scs.ntit.edu.tw/rjwang/. 閱讀建議. VB 提供了多種檔案存取的方式,課本中有詳細介紹,有興趣者可自行研讀 。 上一節著重在“檔案”的角度來介紹文字檔的讀寫。 若從“資料項”的角度來看,本節介紹另一種更簡單的循序文字檔的操作方式。 同學們如果要報考軟體設計丙級,應熟練本節所介紹的檔案操作方式。. 重點提要. 1. 檔案存取函數 2. 文字檔的輸入輸出 3. 實例練習.

osborn
Download Presentation

第 15 章 檔案存取(二)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第 15 章 檔案存取(二) Ren-Jie Wang,王 仁 傑, Ph.D. rjwang@ntit.edu.tw 課程網頁: http://home.scs.ntit.edu.tw/rjwang/

  2. 閱讀建議 • VB提供了多種檔案存取的方式,課本中有詳細介紹,有興趣者可自行研讀。 • 上一節著重在“檔案”的角度來介紹文字檔的讀寫。 • 若從“資料項”的角度來看,本節介紹另一種更簡單的循序文字檔的操作方式。 • 同學們如果要報考軟體設計丙級,應熟練本節所介紹的檔案操作方式。

  3. 重點提要 • 1.檔案存取函數 • 2.文字檔的輸入輸出 • 3.實例練習

  4. 1.檔案存取函數 • Visual Basic提供三種檔案存取類型: • 循序檔案: • 擁有讀取(Input)、寫入(Output)和新增(Append)三種模式,可以將字串資料寫入與讀取文字檔案,例如:記錄檔和一般文字檔案。 • 隨機檔案: • 提供隨機(Random)模式,使用類似資料庫記錄(Records)方式來存取檔案,每一個記錄的長度是相等的。 • 二進位檔案: • 使用二進位(Binary)模式存取檔案的位元組資料,特別適用在可變長度的資料。 • 本節著重介紹循序檔的存取方式

  5. 1.檔案存取函數 • 循序檔案最常的應用是讀寫文字檔案。 • Visual Basic開啟、關閉或讀寫檔案時,都是使用整數的檔案編號值來識別開啟的檔案(因為程式可能同時開啟多個檔案),其相關函數如下 • FreeFile() • 傳回下一個可用的檔案編號(傳回值是整數) • FileOpen(檔案編號, “檔名”, 模式) • 以指定的「檔案編號」和操作「模式」來開啟檔名為“檔名”的檔案。 • FileClose(檔案編號) • 關閉檔案

  6. 1.檔案存取函數 • 開啟檔案: • fn = FreeFile() ‘取得可用檔案編號fn • FileOpen( fn, “檔名”, 模式) • 模式:以循序檔案來說有三種模式 • OpenMode.Iutput 讀取 • OpenMode.Output 覆寫 • OpenMode.Append附加 • 當然也可以自行指定檔案編號,如下所示: • FileOpen(1, “檔名”, OpenMode.Iutput) • 在執行完檔案存取後,請關閉檔案: • FileClose(fn)

  7. 2.文字檔的輸入輸出 • 當使用FileOpen ()函數成功開啟檔案後,我們可以執行相關函數來寫入或讀取檔案內容: • 文字檔的輸入 • Input(檔案編號, 變數名稱) • 自開啟的檔案編號讀取一個資料項,並指派至變數 • 資料項之間必須用「逗號」或「空白」來分隔 • LineInput(檔案編號) • 自開啟的檔案編號讀取單行資料,並指定給 String 變數

  8. 2.文字檔的輸入輸出 • 如果是讀取整個檔案內容,請配合迴圈來處理檔案讀取,如下所示: • Do While Not EOF(fn) ‘未到檔尾則繼續讀 • input(fn, s) • … • Loop

  9. 2.文字檔的輸入輸出 • 文字檔的輸出 • Write、WriteLine 函式 • 將資料寫入循序檔中。以 Write 寫入的資料,通常都會使用 Input 自檔案中讀取。有興趣者自行查閱。 • Print、PrintLine 函式 • 將顯示格式化的資料寫入循序檔中。有興趣者自行查閱。 • My.Computer.FileSystem.WriteAllText() • 可將字串覆寫或附加到檔案原有內容的後面。 • 此法最簡單,推薦使用。

  10. 3. 實例練習 • 以高中生程式解題系統 ZeroJudge之分類題庫 第a001題為例。 學習所有程式語言的第一個練習題: hello請寫一個程式,可以讀入指定的字串,並且輸出指定的字串。 輸入說明 :指定的文字 輸出說明 : 輸出指定的文字 範例輸入 : world C++ mary 範例輸出 : hello, world hello, C++ hello, mary

  11. 3. 實例練習 Dim ans as string=“” ‘答案 Dim s as string ‘開檔 FileOpen( 1, Application.StartupPath & “\a001.in”, OpenMode.Iutput) ‘讀資料項 Do While Not EOF(1) Input(1, s) ‘從檔案編號1的檔案讀入資料項,放在變數s ans &= “hello, ” & s Loop ‘關檔 FileClose() ‘寫出結果檔 My.Computer.FileSystem.WriteAllText(Application.StartupPath & _ “\a001.out”, ans, false) 參考程式碼

More Related