1 / 49

บทที่ 11 ไฟล์ (File)

บทที่ 11 ไฟล์ (File). ประเภทของไฟล์ รูปแบบของไฟล์ การจัดการไฟล์ รูปแบบและการใช้งานฟังก์ชั่นที่เกี่ยวกับไฟล์ การเปิด และ ปิดไฟล์ การจัดการตัวชี้ขอมูลไฟล์ล การเขียนและอ่านไฟล์. 1. ประเภทของไฟล์. ไฟล์ที่ทำหน้าที่รับข้อมูลเข้าหรือส่งข้อมูลออกจากระบบ.

lula
Download Presentation

บทที่ 11 ไฟล์ (File)

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. บทที่ 11 ไฟล์ (File) • ประเภทของไฟล์ • รูปแบบของไฟล์ • การจัดการไฟล์ • รูปแบบและการใช้งานฟังก์ชั่นที่เกี่ยวกับไฟล์ • การเปิด และ ปิดไฟล์ • การจัดการตัวชี้ขอมูลไฟล์ล • การเขียนและอ่านไฟล์

  2. 1. ประเภทของไฟล์ ไฟล์ที่ทำหน้าที่รับข้อมูลเข้าหรือส่งข้อมูลออกจากระบบ ไฟล์ประเภทนี้ ส่วนใหญ่เป็นไฟล์ที่มีทิศทางเดียว คือ รับข้อมูลเข้าทางเดียว หรือส่งข้อมูลออกทางเดียว เช่น คีย์บอร์ด เมาส์ เป็นไฟล์ชนิดรับข้อมูลเข้าทางเดียว จอภาพ เครื่องพิมพ์ เป็นไฟล์ชนิดส่งข้อมูลออกทางเดียว ไฟล์ที่อยู่ในหน่วยความจำสำรอง ข้อมูลและโปรแกรมต่าง ๆ ที่ถูกเก็บไว้ในหน่วยความจำสำรองเป็นไฟล์ที่มีสองทิศทาง คือ ทำหน้าที่ส่งข้อมูล และ รับข้อมูลจากระบบได้ ซึ่งในบทนี้เราจะได้กล่าวถึงเฉพาะไฟล์ประเภทนี้ต่อไปไฟล์ทั้งสองแบบนี้บางครั้งอาจเรียกว่า สตรีม (stream)

  3. 2. รูปแบบของไฟล์ ไบนารี่ไฟล์ (Binary File) คือไฟล์ที่มีการเก็บข้อมูลในรูปแบบเลขฐานสอง เช่น ไฟล์โปรแกรมต่างๆที่มีนามสกุล exe, com, obj เป็นต้น ไฟล์ชนิดนี้โดยปกติ เมื่อเขียนข้อมูลลงไฟล์หรืออ่านข้อมูลจากไฟล์ จะไม่มีการเปลี่ยนแปลงข้อมูล เท็กซ์ไฟล์ (Text File) คือไฟล์ข้อมูลที่มีการเก็บข้อมูลเป็นบรรทัด เช่นไฟล์ข้อความที่สร้างโดยโปรแกรม เอดิเตอร์ ต่างๆ หรือไฟล์นามสกุล c txt bat เป็นต้น เท็กซ์ไฟล์จะถูกเก็บไว้ในหน่วยความจำสำรอง และเก็บในรูปแบบรหัสแอสกี ซึ่งก็คือไฟล์ตัวอักษร

  4. 3. การจัดการไฟล์ 3.1 การเปิดไฟล์ ไฟล์ที่เป็นแหล่งการรับข้อมูลจากสตรีม (stream) มาตรฐาน (เช่น คียบอร์ด) จะถูกเปิดโดยอัตโนมัติอยู่แล้ว ส่วนการเปิดไฟล์กับหน่วยความจำสำรอง เป็นการกำหนดไฟล์แฮนเดิลเพื่อใช้ในการอ้างอิงไฟล์และกำหนดหน่วยความจำส่วนหนึ่งเพื่อใช้สำหรับเป็นที่พักข้อมูลในขณะที่มีการเขียนหรือการอ่านข้อมูล หน่วยความจำส่วนนี้จะเรียกว่า ไฟล์บัฟเฟอร์ (File Buffer) และโดยปกติเมื่อเปิดไฟล์ตัวชี้ข้อมูลไฟล์ก็จะถูกกำหนดให้ชี้ไปที่หน่วยข้อมูลแรกของไฟล์ด้วย

  5. การจัดการไฟล์ (ต่อ) 3.2 การจัดการข้อมูลไฟล์ เขียนหรืออ่านหน่วยข้อมูลไฟล์ เป็นการใส่ข้อมูลลงในหน่วยข้อมูลไฟล์ หรืออ่านข้อมูลจากหน่วยข้อมูลไฟล์ การเลื่อนตัวชี้ข้อมูลไฟล์ไปยังตำแหน่งที่ต้องการ โดยปกติเมื่อมีการเขียนหรืออ่านหน่วยข้อมูลภายในไฟล์ ตัวชี้ข้อมูลไฟล์ จะเลื่อนไปชี้ยังหน่วยข้อมูลถัดไปโดยอัตโนมัติ แต่ในกรณีที่ต้องการเข้าถึงตำแหน่งข้อมูลในจุดที่ไม่ใช่หน่วยถัดไปของข้อมูลที่เพิ่งอ่านหรือเขียน จำเป็นต้องเลื่อนตัวชี้ข้อมูลเอง

  6. การจัดการไฟล์ (ต่อ) • การเขียนหรืออ่านข้อมูลโครงสร้างไฟล์ • ข้อมูลในโครงสร้างไฟล์ประกอบไปด้วยสองส่วนคือ ส่วนที่มีลักษณะเหมือนกัน เพื่อความเป็นมาตรฐานในการติดต่อ และส่วนที่สองเป็นส่วนที่มีลักษณะเฉพาะขึ้นอยู่กับโครงสร้างทางฮาร์ดแวร์ การจัดการในลักษณะนี้เป็นการแก้ไขโครงสร้างของไฟล์ที่นอกเหนือไปจากการอ่านหรือเขียนหน่วยข้อมูล และการเลื่อนตัวชี้ข้อมูลไฟล์ เช่น การหาขนาดของไฟล์ การปรับความสามารถในการเข้าถึงไฟล์เช่นเดียวกับคำสั่ง ATTRIB ในดอส เป็นต้น

  7. การจัดการไฟล์ (ต่อ) • 3.3 การปิดไฟล์ • หลังจากเขียนหรืออ่านข้อมูลแล้วควรปิดไฟล์ เพื่อคืนแฮนเดิลที่จองมาให้แก่ระบบ สำหรับนำไปใช้ในการเข้าถึงไฟล์อื่น ๆ ต่อไป ในกรณีของไฟล์ที่มีการพักข้อมูลในไฟล์บัฟเฟอร์ การปิดไฟล์จะเป็นการนำข้อมูลที่ยังคงค้างอยู่ในไฟล์บัฟเฟอร์ ให้ลงไปในไฟล์ทั้งหมด หากออกจากโปรแกรมโดยไม่ได้มีการปิดไฟล์ จะทำให้ข้อมูลบางส่วนที่ยังคงค้างอยู่ในไฟล์บัฟเฟอร์ ไม่ได้ถูกเขียนลงไฟล์อย่างแท้จริง ซึ่งเป็นเหตุให้ข้อมูลบางส่วนในไฟล์อาจสูญหายได้

  8. การจัดการไฟล์ (ต่อ) • 3.4 สตรีมพอยเตอร์ (Stream Pointer) • สตรีมพอยเตอร์ คือ ค่าการชี้หรือค่าตำแหน่งหน่วยความจำเริ่มต้นของโครงสร้างไฟล์ ซึ่งใช้เป็นที่พักข้อมูลภายในหน่วยความจำหลักของระบบ ระบบปฏิบัติการจะใช้พื้นที่เหล่านี้ในการติดต่อส่งข้อมูลกับไฟล์ในหน่วยความจำสำรอง 3.5 ตัวชี้ข้อมูลไฟล์ (File Pointer) ตัวชี้ข้อมูลไฟล์ เป็นค่าการชี้ที่ชี้ไปยังหน่วยข้อมูลภายในไฟล์ ใช้อ้างถึงตำแหน่งของข้อมูลภายในไฟล์ เพื่อการอ่านหรือการเขียนไฟล์

  9. 4. รูปแบบและการใช้งานฟังก์ชั่นเกี่ยวกับไฟล์ 4.1 การเปิดและปิดไฟล์ ฟังก์ชันที่ใช้ในการเปิดไฟล์ คือ fopen() มีรูปแบบดังนี้ FILE *fopen(const char *filename, const char *mode); FILE ชนิดของตัวแปรไฟล์ filename ชื่อไฟล์ที่ต้องการเปิด mode โหมดในการเปิดไฟล์

  10. การเปิดและปิดไฟล์ (ต่อ) นอกจากโหมดที่กล่าวมาแล้ว ยังมีโหมดในการระบุประเภทไฟล์ที่ต้องการเปิดอีก 2 โหมดคือ สามารถใช้โหมดในการเปิดไฟล์และโหมดในการระบุประเภทไฟล์ร่วมกันได้ เช่น rb หมายถึง การเปิดไบนารีเพื่ออ่าน และ wt+ หมายถึง การเปิดเท็กซ์ไฟล์เพื่อทำการเขียน เป็นต้น ตัวอย่างที่ 11.1 แสดงการเปิดไฟล์ FILE *InStream; InStream = fopen ("a:test.txt" , "wt+");

  11. การเปิดและปิดไฟล์ (ต่อ) ในกรณีที่ไม่สามารถเปิดไฟล์ได้ เช่นไม่มีไฟล์ในระบบ ในกรณีของโหมด ra และ r+ ฟังก์ชัน fopen() จะให้ค่าออกมาเป็น NULL ดังตัวอย่าง if((InStream = fopen ("readme.txt","r")) == NULL) { fprintf (stderr, "No file in System…\n" exit (1); }

  12. การเปิดและปิดไฟล์ (ต่อ) หากต้องการเปลี่ยนโหมดของไฟล์หลังจากเปิดไฟล์มาแล้ว สามารถกระทำได้สองทาง คือ ปิดไฟล์นั้นแล้วเปิดไฟล์ใหม่ ในโหมดที่ต้องการหรืออาจใช้ฟังก์ชัน freopen() FILE *freopen(const char *filename, const char *mode,FILE *stream); การปิดไฟล์ • stream ไฟล์พอยน์เตอร์ที่ได้จากการเปิดไฟล์ ฟังก์ชัน fclose() จะส่งค่า 0 กลับหากสามารถทำการปิดไฟล์ได้ และจะส่งค่าคงที่EOFกลับหากมีข้อผิดพลาดในการปิดไฟล์ เช่น fclose(InStream); • FILE ชนิดของตัวแปรไฟล์ • filenameชื่อไฟล์ที่ต้องการเปิด ซึ่งสามารถกำหนดไดร์ฟและพาทได้ • mode โหมดในการเปิดไฟล์ที่ต้องการเปลี่ยน • stream ไฟล์พอยน์เตอร์ที่ได้จากการเปิดไฟล์ • เช่น • InStream = freopen ("a:test.txt", "rb+" , InStream); int *fclose(FILE *stream);

  13. ตัวอย่างที่ 11.2 การทดสอบการเปิด และ ปิดไฟล์ #include <stdio.h> FILE *testfile; /* Declare stream */ char filename [80]; /* Declare string variable for storing filename * void main (void) { printf("Please enter filename (and directory) "); gets(filename); /* Open file and check file*/ if( (testfile = fopen(filename, "r") ) == NULL) printf("No file %s in system\n",filename); else { printf("File %s is valid\n",filename); /* Close after opening and using */ fclose(testfile); } }

  14. ผลการทำงานของโปรแกรม Please enter filename (and directory) c:\autoexec.bat File c:\autoexec.bat is valid 4.2 การจัดการตัวชี้ข้อมูลไฟล์ int fseek(FILE *stream, long offset,int where); ค่า where คือตำแหน่งอ้างอิง ไฟล์ ในภาษาซีได้มีการกำหนดค่าคงที่ใช้สำหรับบอกตำแหน่งอ้างอิงของข้อมูลไฟล์ไว้ดังนี้

  15. การจัดการตัวชี้ข้อมูลไฟล์ (ต่อ) fseek(InStream,5L,SEEK_CUR); เป็นการเลื่อนตัวชี้ข้อมูลไฟล์ไปทางท้ายไฟล์อีก 5 ไบต์ จากตำแหน่งเดิม ดังรูป ฟังก์ชั่นอีกตัวหนึ่งที่ใช้เลื่อนตัวชี้ข้อมูลไฟล์ไปยังจุดเริ่มต้นของไฟล์ void rewind(FILE *stream); • stream ไฟล์พอยน์เตอร์ที่ได้จากการเปิดไฟล์

  16. 4.3 การเขียนและอ่านไฟล์ • ในการอ่านและเขียนสามารถกระทำได้ทั้งไบนารี่ไฟล์และเท็กซ์ไฟล์ฟังก์ชันมาตรฐาน • ในการเขียนและอ่านไฟล์จะรวบรวมอยู่ที่ stdio.hโดยจะแบ่งระดับ • การอ่านและเขียนออกเป็น 3 ระดับ • 1. ระดับอักขระ (character level) ได้แก่ ฟังก์ชัน • putc(),fputc(),getc(),fgetc() • 2. ระดับข้อความ (string level) ได้แก่ ฟังก์ชัน • fputs(),fprintf(),fgets(),fscanf() • 3. ระดับเรคคอร์ด (record level) ได้แก่ ฟังก์ชัน • fwrite(),fread()

  17. การเขียนและอ่านไฟล์ (ต่อ) int putc(int c, FILE *stream); int fputc(int c,FILE *stream); ฟังก์ชัน putc()และ fputc() จะส่งค่ารหัสแอสกีของ c กลับหากเขียนไฟล์สำเร็จ และจะส่ง EOF กรณีที่มีข้อผิดพลาดในการเขียน • ฟังก์ชันputc()ใช้ในการส่งข้อมูลไปยังสตรีมที่กำหนด • ฟังก์ชัน fputc()ใช้สำหรับเขียนข้อมูลลงในสตรีม int getc(FILE *stream); int fgetc(FILE *stream); ฟังก์ชัน getc()และ fgetc()ส่งรหัสแอสกีของตัวอักษรที่อ่านได้กลับหากอ่านไฟล์สำเร็จ ะส่ง EOFกลับกรณีมีข้อผิดพลาดในการเขียนหรือ ตำแหน่งที่อ่านเป็นจุดสิ้นสุดของไฟล์ • ฟังก์ชันgetc()ใช้ในการอ่านข้อมูลจากสตรีมที่กำหนด • ฟังก์ชัน fgetc() ใช้อ่านข้อมูลจำนวนหนึ่งไบต์จากสตรีม โดยค่าที่ส่งกลับคือ • ค่าข้อมูลที่อ่านได้

  18. การเขียนและอ่านไฟล์ (ต่อ) char *fgets(char *s,int n,FILE *stream); int fputs(const char *s,FILE *stream); • n จำนวนตัวอักษรที่ต้องการอ่านจากไฟล์ • s สตริงบัพเฟอร์ • streamไฟล์พอยน์เตอร์ที่ได้จากการเปิดไฟล์ • ฟังก์ชัน fgets()จะส่งสตริงที่อ่านได้ (ถูกชี้ด้วยตัวแปร s) กลับหากอ่านไฟล์สำเร็จ • และจะส่งNULL กรณีที่มีข้อผิดพลาดในการอ่าน • ฟังก์ชันfgets()ใช้อ่านข้อมูลสตริงจากสตรีม โดยจะหยุดอ่านเมื่อพบ • รหัสขึ้นบรรทัดใหม่ (0x0a) หรือจะหยุดอ่านเมื่ออ่านได้เกินกว่าค่า n-1 ตัวอักษร • ฟังก์ชัน fputs() ใช้เขียนข้อมูลสตริงลงในสตรีม

  19. การเขียนอ่านไฟล์ (ต่อ) int fprintf(FILE *stream,const char *format[,argument,…]); size_t fread(void *ptr,size_t size,size_t n,FILE *stream); size_t fwrite(void *ptr,size_t size,size_t n,FILE *stream); streamไฟล์พอยน์เตอร์ที่ได้จากการเปิดไฟล์ formatรูปแบบของข้อมูลที่จะเขียนลงไฟล์ (เหมือนกับ printf()เช่น %d %f) argumentข้อมูลที่จะเขียนลงไฟล์ ptr พอยน์เตอร์ที่ชี้ไปยังข้อมูลที่อ่านได้จากไฟล์(สำหรับ fread()) พอยน์เตอร์ที่ชี้ไปยังข้อมูลที่จะเขียนลงไฟล์(สำหรับ fwrite()) size ขนาดหนึ่งหน่วยข้อมูลที่จะอ่านหรือเขียน n จำนวนข้อมูลที่จะอ่านหรือเขียน ( แต่ละข้อมูลมีขนาดเท่ากับ size) streamไฟล์พอยน์เตอร์ที่ได้จากการเปิดไฟล์

  20. ตัวอย่างที่ 11.3 แสดงการจัดเก็บข้อมูลสตรัคเจอร์ลงไฟล์ #include<conio.h> #include<stdio.h> #include<stdlib.h> FILE *studentfile; struct studenttype { char name[80]; int age; char sex[8]; }student; void main (void) {char tmp[40]; /* Open file for writing*/ studentfile = fopen ("student.dat","wb");

  21. ตัวอย่างที่ 11.3 แสดงการจัดเก็บข้อมูลสตรัคเจอร์ลงไฟล์ (ต่อ) do{ /* Read name */ printf("Please enter name : "); gets(student.name); /* Read age */ printf("Please enter age : "); gets(tmp); student.age = atoi(tmp); /* Read sex */ printf("Please enter sex : "); gets(student.sex); /* Write data to file */ fwrite(&student,sizeof(struct studenttype),1,studentfile); /* Ask for enter more data */ printf("Press anykey to enter more data or esc to exit\n"); }while( getch() != 0x1b); /* ESC key = 0x1b */ fclose(studentfile); /* Close file */ }

  22. ผลการทำงานของโปรแกรม Please enter age : 27 Please ente sex : Female Press anykey to enter more data or esc to exit Please enter name : nat Please enter age : 25 Please enter sex : Female Press anykey to enter more data or esc exit

  23. ตัวอย่างที่ 11.4 การอ่านข้อมูลข้อมูลสตรัคเจอร์ที่จัดเก็บในไฟล์ #include<conio.h> #include<stdio.h> #include<stdlib.h> FILE *studentfile; /* Declare stream */ struct studenttype{ /* Declare structure */ char name[80]; int age; char sex[8]; }student; void displayall(void); /* Prototype section */ void displaysome(int recordno); void displayrecord(void); /* Function section */

  24. void main ( void) { char ch; char st[40]; /* Open file for read*/ studentfile = fopen(“student.dat”,”rb”); do{ clrscr( ); /* Clear scteen */ printf("Please select\n"); printf(" 1. Display all\n"); printf(" 2. Select record number\n"); printf(" 3. Exit\n"); /* read keyboard */ ch = getch();

  25. switch(ch) { case ‘1‘: displayall();break; case ‘2‘: printf("Please enter record number : "); gets(st); displaysome(atoi(st)); break; case ‘3‘: fclose(studentfile);exit(0); default : printf("Please enter number 1 or 2\n"); } printf("\n\nPress anykey to continue\n"); getch(); /* Wait a key */ }while(1); /* Can’t exit from here */ }

  26. void displayall(void){ rewind(studentfile); /* Move file pointer to beginning of file */ while (!feof(studentfile)) /* Read until EOF reached */ { if(fread(&student,sizeof(struct studenttype),1,studentfile)) displayrecord( ); } } void displaysome(int recordno){ /* Seek from beginning of file */ if(!fseek(studentfile, sizeof(struct studenttype)*(recordno-1),SEEK_SET)) { /* On success fseek ( ) return zero */ if(fread(&student,sizeof(struct studenttype),1,studentfile)) displayrecord( ); else printf("No such record\n"); } }

  27. void displayrecord (void) { printf("=================================\n"); printf("Student name : %s\n",student.name); printf("Age : %2d Sex : %s\n",student.age,student.sex); } ผลการทำงานของโปรแกรม เมื่อเลือกเมนูข้อ 1 (ข้อมูลจากตัวอย่างที่ 11.3) • Please select • 1.Display all • 2.Select record number • 3.Exit • ================================ • Student name : Nicole • Age : 27 Sex : Female • ================================ • Student name : Clinton • Age : 25 Sex : Male

  28. จากโปรแกรมที่ 11.3 มีฟังก์ชั่นเพิมเติมคือ feof int feof(FILE *stream); stream ไฟล์พอยน์เตอร์ที่ได้จากการเปิดไฟล์ ฟังก์ชัน feof() จะส่งกลับค่า 0 เมื่อไฟล์พอยน์เตอร์ชี้ที่ตำแหน่งท้ายไฟล์ และส่งค่าที่ไม่เป็น 0 กรณีที่ไฟล์พอยน์เตอร์ไม่ได้ชี้ที่ตำแหน่งท้ายไฟล์ ฟังก์ชัน feof() จะใช้ในการตรวจสอบว่าไฟล์พอยน์เตอร์ชี้ที่ตำแหน่งท้ายไฟล์หรือไม่

  29. ตัวอย่างที่ 11.5 ตัวอย่างการใช้ fgetc() #include <conio.h> #include <stdio.h> #include <stdlib.h> #define NEWLINE 0x0a /* Newline code */ #define SPACE 0x20 /* Space code */ FILE *textfile; char filename[80]; void main(void) { char ch; printf("Please enter filename to read : "); gets(filename); if( (textfile = fopen(filename, "rt") ) == NULL ) { printf("No file %s in system \n",filename); exit(1); }

  30. while( !feof(textfile) ) { ch = fgetc(textfile); if( ch >= SPACE ) putchar(ch); else if( ch == NEWLINE ) printf("\n"); } fclose(textfile); printf("\n"); }

  31. ผลการทำงานของโปรแกรม Please enter filename to read : c:\autoexec.bat @C:\PROGRA~1\NORTON~1\NAVDX.EXE /Startup SET TEMP=C:\TEMP SET TMP=C:\TEMP SET PATH=E:\CLIPPER\BIN;%PATH% SET LIB=E:\CLIPPERLIB SET INCLUDE=E:\CLIPPER\INCLUDE SET RLL=E:\CLIPPER5\PLL

  32. ตัวอย่างที่11.6 การเปิดไฟล์ในโหมดต่างๆ โดยสามารถใส่ชื่อไฟล์และโหมดได้ #include <stdio.h> #include <conio.h> void main(void) { FILE *fp; char ch, filename[40], mode[4]; while (1) { /* Input filename and mode. */ printf(" \nEnter a filename : "); gets(filename); printf(" \nEnter a mode ( max 3 characters ) : "); gets(mode);

  33. /* Try to open the file. */ if ( (fp = fopen(filename,mode)) != NULL ) { printf(" \n Successful opening %s in mode %s .\n",filename,mode); fclose(fp); puts("Enter x to exit, any other to continue. "); if( (ch = getch()) == 'x') break; else continue; } else

  34. { fprintf( stderr, "\n Error opening file %s in mode %s .\n",filename,mode); puts("Enter x to exit, any other to try again . "); if ( (ch=getch()) == 'x') break; else continue; } } }

  35. ผลการทำงานของโปรแกรม Enter a filename : c:\config.sys Enter a mode ( max 3 characters ) : rt Successful opening c:\config.sys in mode rt. Enter x to exit, any other to continue

  36. ตัวอย่างที่ 11.7 การเขียนไฟล์โดยใช้ฟังก์ชัน fprintf()) #include <stdio.h> #include <stdlib.h> void clear_kb(void); void main(void) { FILE *fp; float data[5]; int count; char filename[20]; puts(" Enter 5 floating point numerical values. "); for ( count = 0; count < 5; count ++ ) scanf("%f", &data[count]); clear_kb(); puts("Enter a name for the file."); gets(filename);

  37. if ( (fp = fopen(filename,"w")) == NULL ) { fprintf(stderr, "Error opening file %s .", filename); exit(1); } for ( count = 0; count < 5; count ++ ) { fprintf(fp, "\nData[%d] = %f ", count, data[count]); fprintf(stdout,"\nData[%d] = %f", count, data[count]); } fclose(fp); } void clear_kb(void)/* Clears stdin of any waiting characters */ { char junk[80]; gets(junk); }

  38. ผลการทำงานของโปรแกรม Enter 5 floating point numerical values 1.1 23 45 2 45.3 Enter a name for the file. Fl.txt Data[0] = 1.100000 Data[1] = 23.000000 Data[2] = 45.000000 Data[3] = 2.000000 Data[4] = 45.299999

  39. ตัวอย่างที่ 11.8 การอ่านไฟล์ข้อมูลโดยใช้ฟังก์ชัน fscanf() #include <stdio.h> #include <stdlib.h> void main(void){ float f1, f2, f3, f4, f5; FILE *fp; if ( (fp = fopen( "INPUT.TXT", "r")) == NULL ) fprintf(stderr," Error opening file ."); else { fscanf(fp, "%f %f %f %f %f", &f1, &f2, &f3, &f4, &f5); printf("The value are %f, %f, %f, %f, and %f.", f1, f2, f3, f4, f5); fclose(fp); } }

  40. ผลการทำงานของโปรแกรม The value are 123.449997, 87.000999, 100.019997, 0.004560, and 1.000500. ตัวอย่างที่ 11.9เขียนไฟล์โดยใช้ฟังก์ชัน fwrite() และ การอ่านไฟล์โดยใช้ฟังก์ชัน fread() #include <stdio.h> #include <stdlib.h> #define SIZE 20 void main(void) { int count, array1[SIZE], array2[SIZE]; FILE *fp;

  41. /* Initialize array[1] */ for ( count = 0; count < SIZE; count ++ ) array1[count] = 2 * count; /* Open a binary mode file. */ if ( (fp = fopen("direct.txt", "wb")) == NULL ) { fprintf(stderr, "error opening file."); exit(1); } /* save array[1] to the file .*/ if ( fwrite(array1, sizeof(int), SIZE, fp) != SIZE) { fprintf(stderr, "Error writing to file ."); exit(1); } fclose(fp);

  42. /* Now open the same file for reading in binary mode */ if ( (fp = fopen("direct.txt", "rb")) == NULL ) { fprintf(stderr, "Error opening file."); exit(1); } /* read the data into array2[]. */ if ( fread(array2, sizeof(int), SIZE, fp) != SIZE ) { fprintf(stderr, "Error reading file . "); exit(1); } fclose(fp);

  43. /* Now display both attrys to show they're the same. */ printf("Write : "); for ( count = 0; count < SIZE; count ++ ) printf("%d ", array1[count]); printf("\nRead : "); for ( count = 0; count < SIZE; count ++ ) printf("%d ", array2[count]); } ผลการทำงานของโปรแกรม Write : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 Read : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38

  44. ตัวอย่างที่ 11.10 การลบไฟล์ออกจากดิสก์โดยใช้ฟังก์ชัน remove() #include <stdio.h> void main(void) { char filename[80]; printf("Enter the filename to delete : "); gets(filename); if ( remove(filename) == 0 ) printf(" The file %s has been deleted . ", filename); else fprintf(stderr, "Error deleteing the file %s . ", filename); }

  45. ผลการทำงานของโปรแกรม Enter the filename to delete : input.txt The file input.txt has been deleted . ตัวอย่างที่ 11.11 การเปลี่ยนชื่อไฟล์โดยใช้ฟังก์ชัน rename() #include <stdio.h> void main(void) { char oldname[80], newname[80]; printf(" Enter current filename : "); gets(oldname); printf(" Enter new name for file : "); gets(newname);

  46. if ( rename(oldname, newname) == 0 ) printf(" %s has been renamed %s . ", oldname, newname ); else fprintf( stderr, "An error has occurred renaming %s. ", oldname); } ผลการทำงานของโปรแกรม Enter current filename : input.txt Enter new name for file : k.txt Input.txt has been renamed k.txt

  47. ตัวอย่างที่ 11.12 โปรแกรมคัดลอกไฟล์ #include <stdio.h> int file_copy( char *oldname, char * newname); void main(void) { char source[80], dest[80]; printf("\n Enter source file : "); gets(source); printf("\n Enter destination file : "); gets(dest); if ( file_copy( source, dest ) == 0 ) puts( "Copy operation successful "); else fprintf(stderr, "Error during copy operation"); }

  48. int file_copy ( char *oldname, char *newname) { FILE *fold, *fnew; int c; if ( (fold = fopen(oldname,"rb")) == NULL ) return -1; if ( (fnew = fopen(newname,"wb")) == NULL ) return -1; while(1) { c = fgetc(fold); if ( !feof( fold )) fputc( c, fnew); else break; } fclose( fnew ); fclose( fold ); return 0; }

  49. ผลการทำงานของโปรแกรมคัดลอกไฟล์ผลการทำงานของโปรแกรมคัดลอกไฟล์ Enter source file : input.txt Enter destination file : cp.txt Copy operation successful

More Related