Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Processing 檔案處理. 檔案 I/O 作業系統的關係 使用者程式 C 標準 I/O 函示庫 UNIXMS_DOS System call.

Similar presentations


Presentation on theme: "File Processing 檔案處理. 檔案 I/O 作業系統的關係 使用者程式 C 標準 I/O 函示庫 UNIXMS_DOS System call."— Presentation transcript:

1 File Processing 檔案處理

2 檔案 I/O 作業系統的關係 使用者程式 C 標準 I/O 函示庫 UNIXMS_DOS System call

3 檔案概念 Thisafie l stream is a opened file C 把檔案看成是字元串列,亦稱為 stream

4 File 型別 Thisafie l file name length R/W file structure DOS 與 I/O 函示 真正的檔案 disk

5 檔案的開啟和關閉 fopen() : #include “stdio.h” char* filename, *mode; FILE *stream; stream = fopen(filename, mode); Mode : r, w, a

6 檔案的開啟和關閉 fclose() : #include “stdio.h” FILE *stream; stream = fclose(stream);

7 DOS 啟動時會固定開啟五個檔案 stdio.h #define _NFILE 20 最多可開啟 20 個檔案 實際只有 15 個檔案 stdin, IN, text stdout, OUT, text stderr, OUT, text stdaux, I/O, binary Stdprn, OUT, binary

8 C 的檔案 I/O 函示群 fgetc() 、 fputc() fgetc(): read a char from a opened stream int r; FILE *stream; r = fgetc(stream); fputc(): write a char to a opened stream int r; FILE *stream; r = fputc(c, stream);

9 C 的檔案 I/O 函示群 fgets() 、 fputs() fgetts(): read a string from a opened stream char *s; int n; FILE *stream; p = fgets(s, n, stream); // if p=s, it is successful. fputs(): write a string to a opened stream char *s; int n; FILE *stream; p = fputs(s, n, stream); // 0x0d ans 0x0a

10 C 的檔案 I/O 函示群 fscanf() 、 fprintf() fscanf(): scanf a string from a opened stream char *cs; int n; FILE *stream; ptr1, ptr2,… n = fscanf(stream, cs, ptr1, ptr2,..,); fprintf(): print a string to a opened stream char *cs; int n; FILE *stream; arg1, arg2,….. n = fprintf(stream, cs, arg1, arg2);

11 循序與隨機讀寫 檔案位址與指位器 Thisafie l pos

12 循序與隨機讀寫 fseek(): move file position pointer long offset; int mode; FILE *stream; int ret; ret = fseek(stream, offset, mode); ret == 0, ret!=0 fail SEEK_SET : from the file head SEEK_CUR : from the current position SEEK_END : from the file end

13 Binary Read and Write size_t fread(void* ptr, szie_t size, size_t nitem, FILE* stream); size_t fwrite(void* ptr, szie_t size, size_t nitem, FILE* stream); void *ptr; // the address of read or write buffer size_t size; // the size of a data item size_t nitem; // the number of data items FILE* stream;

14 Case Study : Phone Book struct record{ int id; char name[20]; char phone[20]; }; Data structure : typedef struct record RD;

15 Case Study : Phone Book void add_record(RD*); void delete_record(RD*); void search_record(RD*); void display_all(RD*); void quit();

16 Case Study : Phone Book RD a[RECORD_NUM]={{0,"",""},}; if( (fp=fopen("myphone.txt", "r")) != NULL){ fread(a, sizeof(RD), RECORD_NUM, fp); fclose(fp); } else { fp=fopen("myphone.txt", "w"); fwrite(a, sizeof(RD), RECORD_NUM, fp); fclose(fp); } Create a phone book

17 Case Study : Phone Book while(1) { clrscr(); printf("WELECOME MY TELEPHONE BOOK !!\n"); printf("1. SAVE A RECORD\n"); printf("2. DELETE A RECORD\n"); printf("3. SEARCH A RECORD\n"); printf("4. DISPLAY ALL RECORD\n"); printf("5. QUIT\n"); printf("YOUR CHOICE(1-5)\n"); scanf("%d", &choice); switch(choice) { case 1: add_record(a); break; case 2: delete_record(a); break; case 3: search_record(a); break; case 4: display_all(a); break; case 5: quit(a); } Menu

18 Case Study : Phone Book while(stop == NO){ clrscr(); printf("ADD or MODIFY A RECORD\n"); printf("Enter ID number (1-100):\n"); scanf("%d", &newrecord.id); printf("Enter USER NAME:\n"); scanf("%s", newrecord.name); printf("Enter USER PHONE NUMBER:\n"); scanf("%s", newrecord.phone); if( a[newrecord.id-1].id != 0) { printf("RECORD%d has exited, do you want to update this record ? \ (NO:0 ; YES:1)\n", newrecord.id); scanf("%d", &ans); if(ans == 1) a[newrecord.id-1] = newrecord; } else a[newrecord.id-1] = newrecord; printf("to continue record addition (press 0) or quit (press 1)\n"); scanf("%d", &stop); } Add_record

19 Case Study : Phone Book while(stop==NO) { clrscr(); printf("DELETE A RECORD\n"); printf("Enter ID number\n"); scanf("%d", &id); a[id-1].id=0; printf("Do you want to continue delete records (press 0) or quit(press 1)\n"); scanf("%d", &stop); } Delete record

20 Case Study : Phone Book while(stop == NO) { id=0; clrscr(); printf("SEARCH A RECORD\n"); printf("Enter USER name:\n"); scanf("%s", name); while( strcmp(name, a[id].name) != 0 && id<RECORD_NUM) id++; if(id<RECORD_NUM) printf("%5d%20s%20s\n", a[id].id, a[id].name, a[id].phone); else printf("NOT FOUND !!\n"); printf("Do you want to continue (press 0) or quit (press 1) ?\n"); scanf("%d", &stop); } Search record

21 Case Study : Phone Book while(stop==NO) { clrscr(); count=0; for(i=0 ; i<RECORD_NUM ; i++) { if( a[i].id != 0){ printf("%5d%25s%25s\n", a[i].id, a[i].name, a[i].phone); count++; } if(count == 25){ getch(); count=0; } printf("Do you want to redisply all records ?(0:YES ; 1:NO)\n"); scanf("%d", &stop); } Display record

22 Case Study : Phone Book void quit(RD* a) { FILE *fp; fp=fopen("myphone.txt","w"); fwrite(a, sizeof(RD), RECORD_NUM, fp); fclose(fp); exit(0); } Quit system


Download ppt "File Processing 檔案處理. 檔案 I/O 作業系統的關係 使用者程式 C 標準 I/O 函示庫 UNIXMS_DOS System call."

Similar presentations


Ads by Google