file processing 檔案處理. 檔案 i/o 作業系統的關係 使用者程式 c 標準 i/o...
TRANSCRIPT
File Processing檔案處理
檔案 I/O作業系統的關係
使用者程式
C標準 I/O函示庫
UNIXMS_DOS
System call
檔案概念
T h i s a f i el
stream is a opened file
C把檔案看成是字元串列,亦稱為 stream
File 型別
T h i s a f i el
file namelengthR/W
file structure
DOS與 I/O函示
真正的檔案 disk
檔案的開啟和關閉
fopen() : #include “stdio.h” char* filename, *mode; FILE *stream;
stream = fopen(filename, mode);
Mode : r, w, a
檔案的開啟和關閉
fclose() : #include “stdio.h” FILE *stream;
stream = fclose(stream);
DOS 啟動時會固定開啟五個檔案
stdio.h
#define _NFILE 20最多可開啟 20個檔案實際只有 15個檔案
stdin , IN, text stdout, OUT, textstderr, OUT, textstdaux, I/O, binaryStdprn, OUT, binary
C的檔案 I/O函示群fgetc()、 fputc()
fgetc(): read a char from a opened streamint r;FILE *stream;r = fgetc(stream);
fputc(): write a char to a opened streamint r;FILE *stream;r = fputc(c, stream);
C的檔案 I/O函示群fgets()、 fputs()
fgetts(): read a string from a opened streamchar *s;
int n;FILE *stream;p = fgets(s, n, stream); // if p=s, it is successful.
fputs(): write a string to a opened streamchar *s;
int n;FILE *stream;p = fputs(s, n, stream); // 0x0d ans 0x0a
C的檔案 I/O函示群fscanf()、 fprintf()
fscanf(): scanf a string from a opened streamchar *cs;
int n;FILE *stream;
ptr1, ptr2,…n = fscanf(stream, cs, ptr1, ptr2,..,);
fprintf(): print a string to a opened streamchar *cs;
int n;FILE *stream;
arg1, arg2,…..n = fprintf(stream, cs, arg1, arg2);
循序與隨機讀寫
檔案位址與指位器
T h i s a f i el
pos
循序與隨機讀寫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 headSEEK_CUR : from the current positionSEEK_END : from the file end
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 buffersize_t size; // the size of a data itemsize_t nitem; // the number of data itemsFILE* stream;
Case Study : Phone Book
struct record{
int id;
char name[20];
char phone[20];
};
Data structure :
typedef struct record RD;
Case Study : Phone Book
void add_record(RD*);
void delete_record(RD*);
void search_record(RD*);
void display_all(RD*);
void quit();
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
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
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
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
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
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
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