งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Lecture no. 10 Files System

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Lecture no. 10 Files System"— ใบสำเนางานนำเสนอ:

1 Lecture no. 10 Files System
C Programming Lecture no. 10 Files System

2 การใช้ file ในภาษาซี ฟังก์ชั่นต่างๆ ในการจัดการ file มีแนวคิดมาจากเรื่อง I/O stream ซึ่งเปรียบเสมือน character array ที่ยาวไม่มีที่สิ้นสุด โดย stream ที่ว่านี้สามารถ access ได้ดังนี้ - โดยใช้ file pointers เป็นการจัดการระดับสูง หรือ buffered - โดยใช้ file descriptors เป็นการจัดการในระดับต่ำ หรือ unbuffered Department of Computer Science C Programming

3 ในกรณีของ file pointer
คือ pointer ที่ชี้ไปยัง data type FILE ซึ่งถูกกำหนดไว้ใน stdio.h โดยเป็น structure ที่เป็นที่เก็บรวบรวมข้อมูลที่เกี่ยวข้องกับ file นั้น เช่น - File descripter - ตำแหน่งปัจจุบันใน buffer - Pointers ไปยัง buffers - ในขณะนั้น file กำลังถูก read หรือ write อยู่หรือไม่ Department of Computer Science C Programming

4 ในกรณีของ file pointer (2)
มี file pointers อยู่ 3 ชนิด ซึ่งมีอยู่ก่อนหน้านั้นแล้ว ได้แก่ - stdin stdout stderr ทั้งนี้ก่อนที่ file จะสามารถถูก read หรือ write จะต้องใช้ฟังก์ชั่นใน C library ชื่อ fopen() เปิด file ไว้ก่อน ซึ่งมี limit ว่าจะสามารถเปิดได้กี่ files พร้อมๆ กัน หลังจากที่ files ถูกใช้เสร็จแล้ว files จะต้องถูกปิดโดยใช้ฟังก์ชั่น fclose() ซึ่งเป็นการเคลียร์ file buffer และตัด connection ไปยัง file นั้นออก   Department of Computer Science C Programming

5 การเปิดไฟล์ (Opening Files)
ใช้ฟังก์ชัน fopen() ซึ่งมีรูปแบบดังนี้ FILE *fopen( char *path, char *mode); ฟังก์ชั่นนี้มี 2 arguments ทั้ง 2 arguments เป็น pointer ไปยัง character strings โดย pointer แรกจะเก็บค่า address ของชื่อ file ที่จะถูกเปิด และ pointer ที่ 2 เป็น address ของ access mode   Department of Computer Science C Programming

6 ตารางต่อไปนี้แสดง ความหมายของ access mode
Content ของ access mode string ความหมาย "w" เปิด file เพื่อ write ซึ่งถ้ามี file นั้นอยู่แล้วการเลือกใช้ mode นี้จะทำการลบ content ของเดิมออก   "r" เปิด file เพื่อ read   "a" เปิด file เพื่อ write ถ้ามี file เดิมอยู่ จะ write ข้อมูลต่อท้าย file เดิม ถ้าฟังก์ชั่น fopen() ทำงานสำเร็จจะ return ค่าของ file pointer แต่หากทำงานไม่สำเร็จ (ไม่สามารถเปิด file ได้) ฟังก์ชั่นจะ return NULL Department of Computer Science C Programming

7 การปิด files (Clossing files)
เมื่อใช้ files แล้วจะต้องปิดด้วยฟังก์ชั่น flose() ซึ่งมีรูปแบบดังนี้ int fclose( FILE *stream); ฟังก์ชั่น fclose() จะ return ค่า 0 หากทำงานสำเร็จ หากทำไม่สำเร็จจะ return ค่าเป็น EOF   Department of Computer Science C Programming

8 ศึกษาการใช้ fopen() และ fclose()
#include <stdio.h> int main(void) { FILE *fptr; fptr = fopen("ascii.c","r"); if(fptr != NULL){ printf("Hay I can open myself\n"); close(fptr); } else printf("Hi my source is lost\n"); return (0); } Department of Computer Science C Programming

9 Character Input และ Output จาก Files
ในการอ่าน character จาก input stream ที่ถูกชี้โดย file pointer สามารถใช้ฟังก์ชั่น getc() และ fgetc() ซึ่งมีรูปแบบของฟังก์ชั่น ดังนี้ int fgetc(FILE *stream); int getc(FILE *stream); โดยหากฟังก์ชั่นทำงานสำเร็จ ฟังก์ชั่นจะคืนค่าเป็น character ในรูปแบบของ integer และหากทำงานไม่สำเร็จจะคืนค่า EOF กลับมา ขอให้ดูวิธีการใช้ฟังก์ชั่นจากโปรแกรมต่อไปนี้ Department of Computer Science C Programming

10 #include <stdio.h> main() { int ch; FILE *fptr;
fptr = fopen("hr12b.c","r"); if(fptr != NULL){ printf("Hay I can open myself\n"); while ((ch = getc(fptr))!=EOF){ printf("%c",ch); } close(fptr); } else printf("Hi my source is lost\n"); } Department of Computer Science C Programming

11 ฟังก์ชั่นสำหรับการ write character ไปสู่ file
int fputc(int c, FILE *stream); int putc(int c, FILE *stream); ฟังก์ชั่นทั้ง 2 มี 2 arguments โดย argument แรกเป็น character ในรูปแบบของ integer ส่วน argument ที่ 2 เป็น file pointer ถ้าฟังก์ชั่นทำงานสำเร็จจะ return character ที่ output ไปในรูปแบบของ integer หากทำงานไม่สำเร็จจะคืนค่า EOF Department of Computer Science C Programming

12 การ copy file #include <stdio.h> main() { int ch;
FILE *infile, *outfile; infile = fopen("hr12c.c","r"); outfile = fopen("hr12c.copy","w"); if((infile == NULL)||(outfile == NULL)) printf("File open error\n"); } Department of Computer Science C Programming

13 การ copy file (2) else { ch = fgetc(infile); while (ch != EOF) {
fputc((char)ch, outfile); } if (infile != NULL) fclose(infile); if (outfile != NULL) fclose(outfile); } Department of Computer Science C Programming

14 Line Oriented Input และ Output กับ Files
มีฟังก์ชั่น ที่เกี่ยวข้องดังต่อไปนี้ int fputs(const char *s, FILE *stream); char *fgets(char *s, int size, FILE *stream); Department of Computer Science C Programming

15 ตัวอย่าง #include <stdio.h> #define SIZE 100 main() {
char *linein, *lineout;   FILE *fp;   fp = fopen("hr12d.c","r");   if (fp == NULL)   printf("Cannot open file\n");   else   Department of Computer Science C Programming

16 ตัวอย่าง (2) do { lineout = fgets(linein,SIZE,fp);
fputs(lineout,stdout); } while (*linein != EOF); close(fp); } โปรแกรมนี้มีอะไรผิดหรือ เหตุใดจึงเกิด Segmentation fault ที่ผลรัน Department of Computer Science C Programming

17 Formatted Input และ Output กับ Files
ใช้ฟังก์ชั่น fprintf() และ fscanf() ซึ่งทำงานเหมือนกับ printf() และ scanf() ยกเว้นมี arguments ตัวแรกเป็น file pointer รูปแบบของฟังก์ชั่นมีดังนี้ int fscanf( FILE *stream, const char *format, ...);   int fprintf( FILE *stream, const char *format, ...);   Department of Computer Science C Programming

18 ตัวอย่าง #include <stdio.h> #define STRSIZE 1000
#define READ "r" void main() { FILE *fp; int wc = 0; int checkread; char word[STRSIZE]; fp = fopen("hr12e.c",READ); if (fp == NULL) fprintf(stderr, "Can't open file"); Department of Computer Science C Programming

19 ตัวอย่าง (2) else { checkread = fscanf(fp,"%s",word);
while (checkread != EOF) { wc++; } fprintf(stdout,"Number of words %d\n",wc); fclose(fp); Department of Computer Science C Programming


ดาวน์โหลด ppt Lecture no. 10 Files System

งานนำเสนอที่คล้ายกัน


Ads by Google