Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Input and Output Chap. 11: 施威銘的書 Chap. 7: K&R.

Similar presentations


Presentation on theme: "File Input and Output Chap. 11: 施威銘的書 Chap. 7: K&R."— Presentation transcript:

1 File Input and Output Chap. 11: 施威銘的書 Chap. 7: K&R

2 Preview Input and output facilities are not part of the C language itself ANSI defines a standard library, including functions to do Input and output. Ex. printf(), scanf() String handling. Ex. strlen() Storage management. Ex. malloc() Mathematical routines. Ex. cos() I/O 不是C語言的一部份 標準函式庫

3 Why standard functions and library?
C program C standard library Different Operating System 作業系統 MS-DOS Windows Unix

4 How can I know which standard functions that I can use?
Book: The Waite Group’s C bible C++ MSDN library Search for functions or objects

5 Different file system under different OS
在不同作業系統下,檔案系統不盡相同,所以要透過 C 標準函式做檔案處理 C program Open a file C standard library fopen, fread,… Operating System 作業系統 MS-DOS Windows Unix ? FAT NTFS File System

6 File access (1) So far, we have learned Standard input from keyboard
Standard output to the screen When a C program is started, the OS opens three files stdin stdout stderr 標準輸出 stdout stdin program stderr 標準輸入 錯誤訊息輸出

7 File access (2) File is taken as stream 不管檔案是如何儲存,當開啟檔案後,檔案內容被視為
Character stream 文字檔 T h i s a t e x t … 非文字檔 BA 2D FE 0A …

8 I/O with buffer (具緩衝區的I/O)

9 3 steps to access a file (3)
Library functions 開啟檔案 Open a file fopen Read/write the opened file fgets fread, fwrite fscanf, fprintf, … 讀/寫檔案 關閉檔案 Close a file fclose

10 Open a file (4) Access mode存取模式 read, write, append Example: Purpose: negotiate with the OS, and the OS returns a file pointer FILE: a structure that contains Location of a buffer Character position in the buffer Read or write mode Whether end of file have occurred File name FILE *fp; fp = fopen(“test.txt”, “rb”);

11 FILE structure

12 Access mode 存取模式 (1/2) (read) (write) (append)

13 Access mode 存取模式 (2/2)

14 fopen: 語法

15

16 Read from a file (5)

17

18

19 Write to a file (6)

20

21 Close a file (7)

22 Notes on final exam. 1/12 room 307, 308, 9:10 am ~ 3:00 pm
Open book, 先寫紙上,再上機 第二次期中考及格與不及格者考題不同 C Programming exam., including all basic C grammars, pointer, structure, file access, etc.


Download ppt "File Input and Output Chap. 11: 施威銘的書 Chap. 7: K&R."

Similar presentations


Ads by Google