49
11-1 บบบบบ 11 บบบบ (File) บบบบบบบบบบบบบ บบบบบบบบบบบบบ บบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบ บบบบบบบบบบบบบบบบบบบบบบ บบบบบบบ บบบ บบบบบบบ บบบบบบบบบบบบบบบบบบบบ บบบบบ บบบบบบบบบบบบบบบบบบบ

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

  • Upload
    lula

  • View
    56

  • Download
    3

Embed Size (px)

DESCRIPTION

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

Citation preview

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

11-1

บทท�� 11 ไฟล์ (File)

• ประเภทของไฟล์• ร�ปแบบของไฟล์• การจั�ดการไฟล์• ร�ปแบบแล์ะการใช้�งานฟ�งกช้��นท��เก��ยวก�บไฟล์

• การเป ด แล์ะ ป ดไฟล์• การจั�ดการตั�วช้�"ขอมู�ล์ไฟล์ล์• การเข�ยนแล์ะอ$านไฟล์

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

11-2

1. ประเภทของไฟล์ไฟล์ท��ท%าหน�าท��ร�บข�อมู�ล์เข�าหร'อส่$งข�อมู�ล์ออกจัากระบบ

ไฟล์�ประเภทนี้� ส่�วนี้ใหญ่�เป�นี้ไฟล์�ท��มี�ท�ศทาง เดี�ยว คื�อ ร บข้#อมี$ล์เข้#าทางเดี�ยว หร�อส่�ง

ข้#อมี$ล์ออกทางเดี�ยว เช่�นี้ คื�ย�บอร�ดี เมีาส่� เป�นี้ไฟล์�ช่นี้�ดีร บข้#อมี$ล์เข้#าทางเดี�ยว จอภาพ

เคืร��องพ�มีพ� เป�นี้ไฟล์�ช่นี้�ดีส่�งข้#อมี$ล์ออกทางเดี�ยว ไฟล์ท��อย�$ในหน$วยความูจั%าส่%ารอง ข้#อมี$ล์แล์ะโปรแกรมีต่�าง ๆ ท��ถู$กเก/บไว#ในี้หนี้�วยคืวามีจ0าส่0ารองเป�นี้ไฟล์�ท��มี�ส่องท�ศทาง คื�อ ท0าหนี้#าท��ส่�งข้#อมี$ล์ แล์ะ ร บข้#อมี$ล์จากระบบไดี# ซึ่2�งในี้บทนี้� เราจะไดี#กล์�าวถู2งเฉพาะไฟล์�ประเภทนี้� ต่�อไปไฟล์�ท งส่องแบบนี้� บางคืร งอาจเร�ยกว�า ส่ตัร�มู (stream)

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

11-3

2. ร�ปแบบของไฟล์ไบนาร��ไฟล์ (Binary File)คื�อไฟล์�ท��มี�การเก/บข้#อมี$ล์ในี้ร$ปแบบเล์ข้ฐานี้

ส่อง เช่�นี้ ไฟล์�โปรแกรมีต่�างๆท��มี�นี้ามีส่ก5ล์ exe, com, obj เป�นี้ต่#นี้ ไฟล์�ช่นี้�ดีนี้� โดียปกต่� เมี��อ

เข้�ยนี้ข้#อมี$ล์ล์งไฟล์�หร�ออ�านี้ข้#อมี$ล์จากไฟล์� จะไมี�มี�การเปล์��ยนี้แปล์งข้#อมี$ล์เท*กซ์ไฟล์ (Text File)

คื�อไฟล์�ข้#อมี$ล์ท��มี�การเก/บข้#อมี$ล์เป�นี้บรรท ดี เช่�นี้ไฟล์�ข้#อคืวามีท��ส่ร#างโดียโปรแกรมี เอดี�เต่อร� ต่�างๆ หร�อไฟล์�นี้ามีส่ก5ล์ c txt batเป�นี้ต่#นี้ เท/กซึ่�ไฟล์�จะถู$กเก/บไว#ในี้หนี้�วยคืวามีจ0าส่0ารอง แล์ะเก/บในี้ร$ปแบบรห ส่แอส่ก� ซึ่2�งก/คื�อไฟล์�ต่ วอ กษร

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

11-4

3. การจั�ดการไฟล์ 31. การเป ดไฟล์ไฟล์�ท��เป�นี้แหล์�งการร บข้#อมี$ล์จากส่ต่ร�มี

(stream) มีาต่รฐานี้ (เช่�นี้ คื�ยบอร�ดี ) จะถู$กเป7ดีโดียอ ต่โนี้มี ต่�อย$�แล์#ว ส่�วนี้การเป ดไฟล์ก�บหน$วยความูจั%าส่%ารอง เป�นี้การก0าหนี้ดีไฟล์�แฮนี้เดี�ล์เพ��อใช่#ในี้การอ#างอ�งไฟล์�แล์ะก0าหนี้ดีหนี้�วยคืวามีจ0าส่�วนี้หนี้2�งเพ��อใช่#ส่0าหร บเป�นี้ท��พ กข้#อมี$ล์ในี้ข้ณะท��มี�การเข้�ยนี้หร�อการอ�านี้ข้#อมี$ล์ หนี้�วยคืวามีจ0าส่�วนี้นี้� จะเร�ยกว�า ไฟล์บ�ฟเฟอร (File

Buffer) แล์ะโดยปกตั-เมู'�อเป ดไฟล์ตั�วช้�"ข�อมู�ล์ไฟล์ก*จัะถู�กก%าหนดให�ช้�"ไปท��หน$วยข�อมู�ล์แรกของไฟล์ด�วย

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

11-5

การจั�ดการไฟล์ (ตั$อ)

3.2 การจั�ดการข�อมู�ล์ไฟล์ เข�ยนหร'ออ$านหน$วยข�อมู�ล์ไฟล์

เป�นี้การใส่�ข้#อมี$ล์ล์งในี้หนี้�วยข้#อมี$ล์ไฟล์�หร�ออ�านี้ข้#อมี$ล์จากหนี้�วยข้#อมี$ล์ไฟล์� การเล์'�อนตั�วช้�"ข�อมู�ล์ไฟล์ไปย�งตั%าแหน$งท��ตั�องการโดียปกต่�เมี��อมี�การเข้�ยนี้หร�ออ�านี้หนี้�วยข้#อมี$ล์

ภายในี้ไฟล์� ต่ วช่� ข้#อมี$ล์ไฟล์� จะเล์��อนี้ไปช่� ย งหนี้�วย ข้#อมี$ล์ถู ดีไปโดียอ ต่โนี้มี ต่� แต่�ในี้กรณ�ท��ต่#องการ

เข้#าถู2งต่0าแหนี้�งข้#อมี$ล์ในี้จ5ดีท��ไมี�ใช่�หนี้�วยถู ดีไป ข้องข้#อมี$ล์ท��เพ��งอ�านี้หร�อเข้�ยนี้ จ0าเป�นี้ต่#องเล์��อนี้

ต่ วช่� ข้#อมี$ล์เอง

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

11-6

การจั�ดการไฟล์ (ตั$อ)

การเข�ยนหร'ออ$านข�อมู�ล์โครงส่ร�างไฟล์

ข้#อมี$ล์ในี้โคืรงส่ร#างไฟล์�ประกอบไปดี#วยส่องส่�วนี้คื�อ ส่�วนี้ท��มี�ล์ กษณะเหมี�อนี้ก นี้ เพ��อคืวามีเป�นี้มีาต่รฐานี้ในี้การต่�ดีต่�อ แล์ะส่�วนี้ท��ส่องเป�นี้ส่�วนี้ท��มี�ล์ กษณะเฉพาะข้2 นี้อย$�ก บโคืรงส่ร#างทางฮาร�ดีแวร� การจ ดีการในี้ล์ กษณะนี้� เป�นี้การแก#ไข้โคืรงส่ร#างข้องไฟล์�ท��นี้อกเหนี้�อไปจากการอ�านี้หร�อเข้�ยนี้หนี้�วยข้#อมี$ล์ แล์ะการเล์��อนี้ต่ วช่� ข้#อมี$ล์ไฟล์� เช่�นี้ การหาข้นี้าดีข้องไฟล์� การปร บคืวามีส่ามีารถูในี้การเข้#าถู2งไฟล์�เช่�นี้เดี�ยวก บคื0าส่ �ง ATTRIBในี้ดีอส่ เป�นี้ต่#นี้

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

11-7

การจั�ดการไฟล์ (ตั$อ)

33. การป ดไฟล์ หล์ งจากเข้�ยนี้หร�ออ�านี้ข้#อมี$ล์แล์#วควรป ดไฟล์ เพ��อคื�นี้แฮนี้เดี�ล์ท��จองมีาให#แก�ระบบ ส่0าหร บนี้0าไปใช่#ในี้การเข้#าถู2งไฟล์�อ��นี้ ๆ ต่�อไป ในี้กรณ�ข้องไฟล์�ท��มี�การพ กข้#อมี$ล์ในี้ไฟล์�บ ฟเฟอร� การป7ดีไฟล์�จะเป�นี้การนี้0าข้#อมี$ล์ท��ย งคืงคื#างอย$�ในี้ไฟล์�บ ฟเฟอร� ให#ล์งไปในี้ไฟล์�ท งหมีดี หากออกจากโปรแกรมีโดียไมี�ไดี#มี�การป7ดีไฟล์� จะท0าให#ข้#อมี$ล์บางส่�วนี้ท��ย งคืงคื#างอย$�ในี้ไฟล์�บ ฟเฟอร� ไมี�ไดี#ถู$กเข้�ยนี้ล์งไฟล์�อย�างแท#จร�ง ซึ่2�งเป�นี้เหต่5ให#ข้#อมี$ล์บางส่�วนี้ในี้ไฟล์�อาจส่$ญ่หายไดี#

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

11-8

การจั�ดการไฟล์ (ตั$อ) 34 ส่ตัร�มูพอยเตัอร (StreamPointer)

ส่ต่ร�มีพอยเต่อร� คื�อ คื�าการช่� หร�อคื�าต่0าแหนี้�งหนี้�วยคืวามีจ0าเร��มีต่#นี้ข้องโคืรงส่ร#างไฟล์� ซึ่2�งใช่#เป�นี้ท��พ กข้#อมี$ล์ภายในี้หนี้�วยคืวามีจ0าหล์ กข้องระบบ ระบบปฏิ�บ ต่�การจะใช่#พ� นี้ท��เหล์�านี้� ในี้การต่�ดีต่�อส่�งข้#อมี$ล์ก บไฟล์�ในี้หนี้�วยคืวามีจ0าส่0ารอง 35 ตั�วช้�"ข�อมู�ล์ไฟล์ (File Pointer) ต่ วช่� ข้#อมี$ล์ไฟล์� เป�นี้คื�าการช่� ท��ช่� ไปย งหนี้�วยข้#อมี$ล์ภายในี้ไฟล์� ใช่#อ#างถู2งต่0าแหนี้�งข้องข้#อมี$ล์ภายในี้ไฟล์� เพ��อการอ�านี้หร�อการเข้�ยนี้ไฟล์�

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

11-9

4. ร�ปแบบแล์ะการใช้�งานฟ�งกช้��นเก��ยวก�บไฟล์ 41. การเป ดแล์ะป ดไฟล์

ฟ;งก�ช่ นี้ท��ใช่#ในี้การเป ดไฟล์ คื�อ fopen() มี�ร$ปแบบดี งนี้�

FILE *fopen(const char *filename, const char *mode);FILE *fopen(const char *filename, const char *mode);

FILE ช่นี้�ดีข้องต่ วแปรไฟล์� filename ช่��อไฟล์�ท��ต่#องการเป7ดี mode โหมีดีในี้การเป7ดีไฟล์� รห�ส่

โหมูดความูหมูาย

r เป7ดีเพ��ออ�านี้ไฟล์�เก�าw เป7ดีเพ��อเข้�ยนี้ไฟล์�ใหมี� หร�อเข้�ยนี้ท บ

ไฟล์�เก�าa เป7ดีเพ��อเข้�ยนี้ต่�อท#ายข้#อมี$ล์ส่5ดีท#าย

ข้องไฟล์�เก�าr+ เป7ดีเพ��ออ�านี้หร�อเข้�ยนี้ท บไฟล์�เก�าw+ เป7ดีเพ��ออ�านี้หร�อเข้�ยนี้ท บไฟล์�เก�า

หร�อไฟล์�ใหมี�a+ เป7ดีเพ��อเข้�ยนี้ต่�อท#ายไฟล์�เก�า หร�อ

ไฟล์�ใหมี�

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

11-10

การเป ดแล์ะป ดไฟล์ (ตั$อ)

รห�ส่โหมูด

ความูหมูาย

b เป7ดีไฟล์�ประเภทไบนี้าร�ไฟล์�t เป7ดีไฟล์�ประเภทเทกซึ่�ไฟล์�

นี้อกจากโหมีดีท��กล์�าวมีาแล์#ว ย งมี�โหมีดีในี้การระบ5ประเภทไฟล์�ท��ต่#องการ เป7ดีอ�ก 2 โหมีดีคื�อ

ส่ามีารถูใช่#โหมีดีในี้การเป7ดีไฟล์�แล์ะโหมีดีในี้การระบ5ประเภทไฟล์�ร�วมีก นี้ไดี# เช่�นี้ rb หมีายถู2ง การเป7ดีไบนี้าร�เพ��ออ�านี้ แล์ะ wt+ หมีายถู2ง การเป7ดีเท/กซึ่�ไฟล์�เพ��อท0าการเข้�ยนี้ เป�นี้ต่#นี้ ตั�วอย$างท�� 111. แส่ดงการเป ดไฟล์

FILE * InStream; InStream = fopen ("a:test.txt" ,

"wt+");

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

11-11

การเป ดแล์ะป ดไฟล์ (ตั$อ)

ในี้กรณ�ท��ไมี�ส่ามีารถูเป7ดีไฟล์�ไดี# เช่�นี้ไมี�มี�ไฟล์�ในี้ระบบ ในี้กรณ�ข้องโหมีดี ra แล์ะ r+ ฟ;งก�ช่ นี้

fopen() จะให#คื�าออกมีาเป�นี้ NULL ดี งต่ วอย�าง if((InStream = fopen

("readme.txt","r")) == NULL){

fprintf (stderr, "No file in System…\n"

exit (1);}

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

11-12

FILE ช่นี้�ดีข้องต่ วแปรไฟล์�filename ช่��อไฟล์�ท��ต่#องการเป7ดี ซึ่2�งส่ามีารถูก0าหนี้ดีไดีร�ฟแล์ะพาทไดี#mode โหมีดีในี้การเป7ดีไฟล์�ท��ต่#องการเปล์��ยนี้stream ไฟล์�พอยนี้�เต่อร�ท��ไดี#จากการเป7ดีไฟล์�

เช่�นี้ InStream = freopen ("a:test.txt", "rb+" , InStream);

หากต่#องการเปล์��ยนี้โหมีดีข้องไฟล์�หล์ งจากเป7ดีไฟล์�มีาแล์#ว ส่ามีารถูกระท0าไดี#ส่องทาง คื�อ ป7ดีไฟล์�นี้ นี้แล์#วเป7ดีไฟล์�ใหมี� ในี้โหมีดีท��ต่#องการหร�ออาจใช่#ฟ;งก�ช่ นี้ freopen()

การเป ดแล์ะป ดไฟล์ (ตั$อ)

FILE *freopen(const char *filename, const char *mode,FILE *stream);FILE *freopen(const char *filename, const char *mode,FILE *stream);

int *fclose(FILE *stream);int *fclose(FILE *stream);

การป ดไฟล์

• stream ไฟล์�พอยนี้�เต่อร�ท��ไดี#จากการเป7ดีไฟล์�ฟ;งก�ช่ นี้ fclose() จะส่�งคื�า 0 กล์ บหากส่ามีารถูท0าการป7ดีไฟล์�ไดี# แล์ะจะส่�งคื�าคืงท�� EOF กล์ บหากมี�ข้#อผิ�ดีพล์าดีในี้การป7ดีไฟล์�

เช่�นี้ fclose(InStream);

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

11-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); }}

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

11-14

Please enter filename (and directory) c:\autoexec.batFile c:\autoexec.bat is valid

ผล์การท%างานของโปรแกรมู

4.2 การจั�ดการตั�วช้�"ข�อมู�ล์ไฟล์

int fseek(FILE *stream, long offset,int where);int fseek(FILE *stream, long offset,int where);

คื�า where คื�อต่0าแหนี้�งอ#างอ�ง ไฟล์� ในี้ภาษาซึ่�ไดี#มี�การก0าหนี้ดีคื�าคืงท��ใช่#ส่0าหร บบอกต่0าแหนี้�ง อ#างอ�งข้องข้#อมี$ล์ไฟล์�ไว#ดี งนี้�

ช้'�อค$าคงท�� ใช้�แทนตั%าแหน$งSEEK_SET ต่0าแหนี้�งเร��มีต่#นี้ไฟล์�SEEK_CUR ต่0าแหนี้�งท��ต่ วช่� ข้#อมี$ล์ไฟล์�ช่� อย$�ในี้ป;จจ5บ นี้SEEK_END ( )ต่0าแหนี้�งท#ายไฟล์� ต่0าแหนี้�งส่� นี้ส่5ดีไฟล์�

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

11-15

EOF

ห วไฟล์�

ต่0าแหนี้�งเดี�มี ต่0าแหนี้�งใหมี�

ท#ายไฟล์�

การจั�ดการตั�วช้�"ข�อมู�ล์ไฟล์ (ตั$อ)

fseek(InStream,5L,SEEK_CUR);

เป1นการเล์'�อนตั�วช้�"ข�อมู�ล์ไฟล์ไปทางท�ายไฟล์อ�ก 5 ไบตั จัากตั%าแหน$งเด-มู ด�งร�ป

stream ไฟล์พอยนเตัอรท��ได�จัากการเป ดไฟล์

ฟ�งกช้��นอ�กตั�วหน2�งท��ใช้�เล์'�อนตั�วช้�"ข�อมู�ล์ไฟล์ไปย�งจั3ดเร-�มูตั�นของไฟล์

void rewind(FILE *stream);void rewind(FILE *stream);

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

11-16

4.3 การเข�ยนแล์ะอ$านไฟล์ในี้การอ�านี้แล์ะเข้�ยนี้ส่ามีารถูกระท0าไดี#ท งไบนี้าร��ไฟล์�แล์ะเท/กซึ่�ไฟล์� ฟ;งก�ช่ นี้มีาต่รฐานี้ในี้การเข้�ยนี้แล์ะอ�านี้ไฟล์�จะรวบรวมีอย$�ท�� stdio.h โดียจะแบ�งระดี บการอ�านี้แล์ะเข้�ยนี้ออกเป�นี้ 3 ระดี บ

1. ระด�บอ�กขระ (character level) ได�แก$ ฟ�งกช้�น putc(),fputc(),getc(),fgetc()

2 . ระด�บข�อความู (string level) ได�แก$ ฟ�งกช้�น fputs(),fprintf(),fgets(),fscanf()

3. ระด�บเรคคอรด (record level) ได�แก$ ฟ�งกช้�น fwrite(),fread()

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

11-17

การเข�ยนแล์ะอ$านไฟล์ (ตั$อ)

ฟ;งก�ช่ นี้ putc() แล์ะ fputc() จะส่�งคื�ารห ส่แอส่ก�ข้อง c กล์ บหากเข้�ยนี้ไฟล์�ส่0าเร/จ แล์ะจะส่�ง EOF กรณ�ท��มี�ข้#อผิ�ดีพล์าดีในี้การเข้�ยนี้

ฟ�งกช้�น putc() ใช้�ในการส่$งข�อมู�ล์ไปย�งส่ตัร�มูท��ก%าหนดฟ�งกช้�น fputc() ใช้�ส่%าหร�บเข�ยนข�อมู�ล์ล์งในส่ตัร�มู

ฟ;งก�ช่ นี้ getc()แล์ะ fgetc()ส่�งรห ส่แอส่ก�ข้องต่ วอ กษรท��อ�านี้ไดี#กล์ บหากอ�านี้ไฟล์�ส่0าเร/จ ะส่�ง EOF กล์ บกรณ�มี�ข้#อผิ�ดีพล์าดีในี้การเข้�ยนี้หร�อต่0าแหนี้�งท��อ�านี้เป�นี้จ5ดีส่� นี้ส่5ดีข้องไฟล์�

ฟ�งกช้�น getc() ใช้�ในการอ$านข�อมู�ล์จัากส่ตัร�มูท��ก%าหนด ฟ�งกช้�น fgetc() ใช้�อ$านข�อมู�ล์จั%านวนหน2�งไบตัจัากส่ตัร�มู โดยค$าท��ส่$งกล์�บค'อค$าข�อมู�ล์ท��อ$านได�

int putc(int c, FILE *stream); int fputc(int c, FILE *stream); int putc(int c, FILE *stream); int fputc(int c, FILE *stream);

int getc(FILE *stream); int fgetc(FILE *stream); int getc(FILE *stream); int fgetc(FILE *stream);

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

11-18

การเข�ยนแล์ะอ$านไฟล์ (ตั$อ)

n จั%านวนตั�วอ�กษรท��ตั�องการอ$านจัากไฟล์

s ส่ตัร-งบ�พเฟอรstream ไฟล์พอยนเตัอรท��ได�จัากการเป ดไฟล์ฟ�งกช้�น fgets()จะส่�งส่ต่ร�งท��อ�านี้ไดี# (ถู$กช่� ดี#วยต่ วแปร s ) กล์ บหากอ�านี้ไฟล์�ส่0าเร/จ

แล์ะจะส่�ง NULL กรณ�ท��มี�ข้#อผิ�ดีพล์าดีในี้การอ�านี้ฟ�งกช้�น fgets() ใช่#อ�านี้ข้#อมี$ล์ส่ต่ร�งจากส่ต่ร�มี โดียจะหย5ดีอ�านี้เมี��อพบรห ส่ข้2 นี้บรรท ดีใหมี� (0x0a) หร�อจะหย5ดีอ�านี้เมี��ออ�านี้ไดี#เก�นี้กว�าคื�า n-1 ต่ วอ กษร ฟ;งก�ช่ นี้ fputs() ใช่#เข้�ยนี้ข้#อมี$ล์ส่ต่ร�งล์งในี้ส่ต่ร�มี

char *fgets(char *s,int n,FILE *stream); int fputs(const char *s,FILE *stream); char *fgets(char *s,int n,FILE *stream); int fputs(const char *s,FILE *stream);

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

11-19

การเข�ยนอ$านไฟล์ (ตั$อ)

stream ไฟล์พอยนเตัอรท��ได�จัากการเป ดไฟล์format ร�ปแบบของข�อมู�ล์ท��จัะเข�ยนล์งไฟล์ (เหมู'อนก�บ printf() เช้$น %d %f)

argument ข�อมู�ล์ท��จัะเข�ยนล์งไฟล์ptr พอยนเตัอรท��ช้�"ไปย�งข�อมู�ล์ท��อ$านได�จัากไฟล์(ส่%าหร�บ fread())

พอยนเตัอรท��ช้�"ไปย�งข�อมู�ล์ท��จัะเข�ยนล์งไฟล์(ส่%าหร�บ fwrite())

size ขนาดหน2�งหน$วยข�อมู�ล์ท��จัะอ$านหร'อเข�ยน n จั%านวนข�อมู�ล์ท��จัะอ$านหร'อเข�ยน ( แตั$ล์ะข�อมู�ล์มู�ขนาดเท$าก�บ size)

stream ไฟล์พอยนเตัอรท��ได�จัากการเป ดไฟล์

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);

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);

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

11-20

ตั�วอย$างท�� 11.3 แส่ดงการจั�ดเก*บข�อมู�ล์ส่ตัร�คเจัอรล์งไฟล์

#include<conio.h>#include<stdio.h>#include<stdlib.h>

* ; struct studenttype {

[ 8 0 ] ; int age; 8char sex[ ];} ;

((((((( ) {char tmp[40]; /*

*Open file for writing / studentfile = fopen

(" ." ," " );

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

11-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 / (&,(

1studenttype), ,studentfile); * */ Ask for enter more data / printf("Press anykey to enter more data or esc to exit\n");

}while( getch() != 0 x1 b); /* ESC key = 0 x1 b * / * *fclose(studentfile); / Close file / }

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

11-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

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

11-23

ตั�วอย$างท�� 114 การอ�านี้ข้#อมี$ล์ข้#อมี$ล์ส่ต่ร คืเจอร�ท��จ ดีเก/บในี้ไฟล์�

#include<conio.h>#include<stdio.h>#include<stdlib.h>

( (((((( * ; /* *stream / *struct studenttype{ / Declare

*structure / 80char name[ ]; int age; 8char sex[ ];} ;

*void displayall(void); / ( (* /

void displaysome(int recordno); (((((((((((((((((((( (( *

*Function section /

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

11-24

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

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

11-25

() { ‘ 1 ‘: ();; 2case ‘ ‘: printf("Please enter record number :

"); gets(st);

displaysome(atoi(st)); break;

3 0case ‘ ‘: fclose(studentfile);exit( ); 1 2default : printf("Please enter number or \

n");}

printf("\n\nPress anykey to continue\n"); * *getch(); / Wait a key /

}while(1 ); /* Can’t exit from here * /}

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

11-26

( ){ rewind(studentfile); /* Move file pointer to beginning of file * / while (!feof(studentfile)) /* Read until EOF reached * / { 1if(fread(&student,sizeof(struct studenttype), ,studentfile)) displayrecord( ); } }

void displaysome(int recordno){ * */ Seek from beginning of file / if(!fseek(studentfile, sizeof(struct

-* 1studenttype) (recordno ),SEEK_SET)) * *{ / On success fseek ( ) return zero /

1if(fread(&student,sizeof(struct studenttype), ,studentfile)) ( );

(((( printf("No such record\n"); } }

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

11-27

((((((( ) { printf("=================================\n");

printf("Student name : %s\n",student.name); 2printf("Age : % d Sex : %s\n",student.age,student.sex);}

Please select1.Display all2.Select record number3.Exit

================================ Student name : Nicole Age : 27 Sex : Female ================================ Student name : Clinton Age : 25 Sex : Male

ผล์การท%างานของโปรแกรมู เมู'�อเล์'อกเมูน�ข�อ 1 ( ข้#อมี$ล์จากต่ วอย�างท�� 11.3)

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

11-28

int feof(FILE *stream);

int feof(FILE *stream);

ฟ�งกช้�น feof() จัะส่$งกล์�บค$า 0 เมู'�อไฟล์พอยนเตัอรช้�"ท��ตั%าแหน$งท�ายไฟล์ แล์ะส่$งค$าท��ไมู$เป1น 0 กรณี�ท��ไฟล์พอยนเตัอรไมู$ได�ช้�"ท��ตั%าแหน$งท�ายไฟล์

ฟ�งกช้�น feof() จัะใช้�ในการตัรวจัส่อบว$าไฟล์พอยนเตัอรช้�"ท��ตั%าแหน$งท�ายไฟล์หร'อไมู$

stream ไฟล์พอยนเตัอรท��ได�จัากการเป ดไฟล์

จัากโปรแกรมูท�� 113. มู�ฟ�งกช้��นเพ-มูเตั-มูค'อ feof

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

11-29

ตั�วอย$างท�� 1 1.5 ตั�วอย$างการใช้� fgetc()

#include <conio.h> #include <stdio.h> #include <stdlib.h>

# 0 0 *define NEWLINE x a / *Newline code /

# 0 20 *define SPACE x / *Space code / *FILE textfile; char80filename[ ];

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

" , ) ; 1exit( ); }

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

11-30

while( !feof(textfile) ) { ch = fgetc(textfile); if( ch >= SPACE ) putchar(ch); else if( ch == NEWLINE )

printf("\n"); } fclose(textfile); printf("\n");}

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

11-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

ผล์การท%างานของโปรแกรมู

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

11-32

ตั�วอย$างท��11.6 การเป7ดีไฟล์�ในี้โหมีดีต่�างๆ โดียส่ามีารถูใส่�ช่��อไฟล์�แล์ะโหมีดีไดี# # include <stdio.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);

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

11-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; (((( continue; } ((((

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

11-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; (((( continue; } }}

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

11-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

ผล์การท%างานของโปรแกรมู

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

11-36

ตั�วอย$างท�� 117 การเข�ยนไฟล์โดยใช้�ฟ�งกช้�น fprintf())

# include <stdio.h> #include <stdlib.h>

void clear_kb(void); void main(void)

{ FILE *fp; [ 5 ] ; ;float data int count 20char filename[ ]; puts(" Enter 5 floating point

numerical values. "); 0 5for ( count = ; count < ;

count++ ) scanf("%f", &data[count]); clear_kb(); puts("Enter a name for the. " ) ; gets(filename);

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

11-37

if ( (fp = fopen(filename,"w")) == NULL ) { fprintf(stderr, "Error opening file %s .", filename); 1exit( ); } 0 5for ( count = ; count < ; 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 * / { [ 8 0 ] ;

gets(junk);}

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

11-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

ผล์การท%างานของโปรแกรมู

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

11-39

ตั�วอย$างท�� 118 การอ�านี้ไฟล์�ข้#อมี$ล์โดียใช่#ฟ;งก�ช่ นี้ fscanf() #include <stdio.h> #include <stdlib.h>

void main(void){ 1 2 3 4 5float f , f , f , f , f ; FILE*fp; if ( (fp = fopen( "INPUT.TXT", "r")) ==

NULL ) fprintf(stderr," Error opening file ."); (((( 1{ fscanf(fp, "%f %f %f %f %f", &f , 2 3 4 5&f , &f , &f , &f ); printf("The value are %f, %f, %f, %f,

1 2 3 4 5and %f.", f , f , f , f , f ); fclose(fp); }}

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

11-40

ผล์การท%างานของโปรแกรมู

The value are 123.449997, 87.000999, 100.019997, 0.004560, and 1.000500.ตั�วอย$างท�� 119 เข้�ยนี้ไฟล์�โดียใช่#ฟ;งก�ช่ นี้ fwrite() แล์ะ การอ�านี้ไฟล์�โดีย

ใช่#ฟ;งก�ช่ นี้ fread() #include <stdio.h> #include <stdlib.h>

# 20define SIZE void main(void)

1 2 *{ int count, array [SIZE], array [SIZE]; FILE fp;

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

11-41

* 1 */ Initialize array[ ] / 0for ( count = ; count < SIZE; count +

+ ) 1 2*array [count] = count; * */ Open a binary mode file. / if ( (fp = fopen("direct.txt", "wb")) ==

NULL ) { fprintf(stderr, "error opening file."); 1exit( ); } * 1 */ save array[ ] to the file . / 1if ( fwrite(array , sizeof(int), SIZE, fp)

!= ) { fprintf(stderr, "Error writing to file."); 1exit( ); } fclose(fp);

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

11-42

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

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

11-43

* */ Now display both attrys to show they're the same. / printf("Write : "); 0for ( count = ; count < SIZE; count ++ ) 1printf("%d ", array [count]); printf("\nRead : "); 0for ( count = ; count < SIZE; count ++ ) 2printf("%d ", array [count]);}

Write : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38Read : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38

ผล์การท%างานของโปรแกรมู

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

11-44

ตั�วอย$างท�� 1110 การล์บไฟล์ออกจัากด-ส่กโดยใช้�ฟ�งกช้�น remove() #include <stdio.h>

void main(void){ 80char filename[ ]; printf("Enter the filename to delete : "); gets(filename); 0if ( remove(filename) == ) printf(" The file %s has been deleted . ", filename); (((( fprintf(stderr, "Error deleteing the file %s . ", filename);}

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

11-45

Enter the filename to delete : input.txt The file input.txt has been deleted .

ผล์การท%างานของโปรแกรมู

ตั�วอย$างท�� 1111. การเปล์��ยนช้'�อไฟล์โดยใช้�ฟ�งกช้�น rename() #include <stdio.h> void main(void)

80{ char oldname[ ],newname[8 0 ]; printf(" Enter current

filename : "); gets(oldname); printf(" Enter new name for file : ");

gets(newname);

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

11-46

0if ( rename(oldname, newname) == ) printf(" %s has been renamed %s . ", oldname, newname ); (((( 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

ผล์การท%างานของโปรแกรมู

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

11-47

ตั�วอย$างท�� 11.12 โปรแกรมีคื ดีล์อกไฟล์�

#include <stdio.h> *int file_copy( char oldname, char * newname);

void main(void) 80 80{ char source[ ], dest[ ];

printf("\n Enter source file : "); gets(source); printf("\n Enter destination file : "); gets(dest);

0if ( file_copy( source, dest ) == ) puts( "Copy operation

successful "); (((( fprintf(stderr, "Error during

copy operation"); }

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

11-48

int file_copy ( char * oldname, *char newname)

{ FILE *fold, *fnew; int c; (( ( ((((( (

fopen(oldname,"rb")) == NULL ) -return1; (( ( ((((( (

fopen(newname,"wb")) == NULL) -return1; while(1) { c = fgetc(fold); if ( !feof( fold )) fputc( c, fnew); else break; } fclose( fnew ); fclose( fold ); 0;}

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

11-49

Enter source file : input.txt

Enter destination file : cp.txt Copy operation successful

ผล์การท%างานของโปรแกรมูค�ดล์อกไฟล์