មេរៀនទី១០: រក្សារទុកទិន្នន័យក្នុង File
I. Definition file: File
ជាសំណុំនៃទិន្នន័យ ដែលមានទំនាក់ ទំនង ជាមួយគ្នា
និងមានប្រភេទដូចគ្នាត្រូវបាន ផ្ដុំបញ្ចូលគ្នាបង្កើតបាន ជា array of data ។
file ត្រូវបានផ្ទុកក្នុង Disk … ។
II. Analysis file: បណ្ដាជំហ៊ានដើម្បី analysis file រួមមាន ៖
- Declaration file variable :
- Open file for read or write :
- Read file, write file :
- Close file.
II.1). Declaration file variable: យើងកំណត់ប្រើ fptr ជាឈេμាះ pointer file ដើម្បីធ្វើជាឧទាហរណ៍ ។ ក្នុងជាក់ ស្ដែង,
យើងអាចជំនួស fptr ដោយឈេμាះណាផ្សេងទៀតក៏បាន ។
Statement Declaration មាន Syntax ដូចខាងក្រោម ៖
- សញ្ញា * បញ្ជាក់ថា pointer ។
II.2). Open file for write or read : Syntax open file for write:
fptr = fopen (“filename” , “w” );
fptr = fopen (“c:\\TC\\ EXAM.TXT”, “w”);
+ ប្រភេទ analysis file មានរាងដូចខាងក្រោម ៖
- ចំពោះ text file :
“r” : បើក file ដែលមានស្រាប់សំរាប់ read (“read only”) ។
“w” : បើក file ថμីដើម្បី write (“write only”), បើឈេμាះ file ដូចឈេμាះ file ចាស់នោះ file ចាស់ត្រូវបានលប់ចោល ។
“a” : បើក file ដែលមានស្រាប់សំរាប់ write ថែមទិន្នន័យ ។
“r+” : បើក file ដែលមានស្រាប់សំរាប់ read និង write ។
“w+” : បើក file ថμីដើម្បី write និង read
“a+” : បើក file ដែលមានស្រាប់សំរាប់ read និង write ថែមទិន្នន័យឭ
- ចំពោះ binary file : “rb” : បើក binary file ដែលមានស្រាប់សំរាប់ read, “read only,binary file”.
“ wb”: បើក binary file ថμីដើម្បី write, “writing only, binary file”
“ab” : បើក binary file ដែលមានស្រាប់សំរាប់ write data ថែម “r+b” : បើក binary file ដែលមានស្រាប់សំរាប់ read និង write “w+b”: បើក binary file ថμីសំរាប់ read និង write
II. Analysis file: បណ្ដាជំហ៊ានដើម្បី analysis file រួមមាន ៖
- Declaration file variable :
- Open file for read or write :
- Read file, write file :
- Close file.
II.1). Declaration file variable: យើងកំណត់ប្រើ fptr ជាឈេμាះ pointer file ដើម្បីធ្វើជាឧទាហរណ៍ ។ ក្នុងជាក់ ស្ដែង,
យើងអាចជំនួស fptr ដោយឈេμាះណាផ្សេងទៀតក៏បាន ។
Statement Declaration មាន Syntax ដូចខាងក្រោម ៖
FILE *fptr ;
- FILE ជា Keyword ជានិច្ចជាកាលត្រូវតែមាន និងសរសេរជាអក្សរ ធំ។- សញ្ញា * បញ្ជាក់ថា pointer ។
II.2). Open file for write or read : Syntax open file for write:
fptr = fopen (“filename” , “w” );
Syntax open file for read :
fptr = fopen (“filename” , “r” );
ឧទាហរណ៍ៈ បើក file ថμីដើម្បី write data (ដោយ file មានឈេμាះ និង path)fptr = fopen (“c:\\TC\\ EXAM.TXT”, “w”);
+ ប្រភេទ analysis file មានរាងដូចខាងក្រោម ៖
- ចំពោះ text file :
“r” : បើក file ដែលមានស្រាប់សំរាប់ read (“read only”) ។
“w” : បើក file ថμីដើម្បី write (“write only”), បើឈេμាះ file ដូចឈេμាះ file ចាស់នោះ file ចាស់ត្រូវបានលប់ចោល ។
“a” : បើក file ដែលមានស្រាប់សំរាប់ write ថែមទិន្នន័យ ។
“r+” : បើក file ដែលមានស្រាប់សំរាប់ read និង write ។
“w+” : បើក file ថμីដើម្បី write និង read
“a+” : បើក file ដែលមានស្រាប់សំរាប់ read និង write ថែមទិន្នន័យឭ
- ចំពោះ binary file : “rb” : បើក binary file ដែលមានស្រាប់សំរាប់ read, “read only,binary file”.
“ wb”: បើក binary file ថμីដើម្បី write, “writing only, binary file”
“ab” : បើក binary file ដែលមានស្រាប់សំរាប់ write data ថែម “r+b” : បើក binary file ដែលមានស្រាប់សំរាប់ read និង write “w+b”: បើក binary file ថμីសំរាប់ read និង write
“a+b” : បើក binary file ដែលមានស្រាប់ដើម្បី write ថែមទិន្នន័យថμី
II.3). Read file និង Write file ក្នុងជំហ៊ាននេះមានភាព
ខុសគ្នា រវាងបណ្ដាប្រភេទរបស់ file និងប្រភេទ analysis file
។ដូច្នេះយើងត្រូវ សិក្សាក្នុងឧទាហរណ៍ជាមួយបណ្ដា function write, read ។
II.4). Close file: Close file ជាការងារចុងក្រោយគេបង្អស់ក្នុងការ analysis file ។ មានរាង ៖
fclose (fptr);
# include <stdio.h>
main ( )
{FILE *fptr;
fptr = fopen (“sample.dat”, “w”);
if (fptr = = NULL) printf (“\n ERROR – Cannot open”);
else { … /* analysis file */; … fclose (fptr);
}
}
ឧទាហរណ៍ ៖ បង្កើត file នៃចំនួនគត់ ៖
# include <stdio.h>
# include <conio.h>
main ( )
{ char filename[21];
int i;
long num; FILE *f1;
printf (“\n Name file:”); scanf (“%20s”, &filename);
f1 = fopen (filename, “wb”); /*open file for writing only */
/* write 100 number into file */
for ( i = 1; i <= 100; i ++)II.4). Close file: Close file ជាការងារចុងក្រោយគេបង្អស់ក្នុងការ analysis file ។ មានរាង ៖
fclose (fptr);
# include <stdio.h>
main ( )
{FILE *fptr;
fptr = fopen (“sample.dat”, “w”);
if (fptr = = NULL) printf (“\n ERROR – Cannot open”);
else { … /* analysis file */; … fclose (fptr);
}
}
III. Binary file :
III.1) ក្រោយពីបើក file
ថμីជាមួយ analysis file “w”(writing), ដំបូងនោះ file ទទេ ព្រោះមិនទាន់
មានធាតុណាទាំងអស់, ពេលនោះ pointer file នឹង point to ចុង file គឺ (EOF)
ខាងក្រោយនេះ ជា ឧទាហរណ៍ បង្កើត file មាន 1០០ ធាតុ ដែលមានតំលៃពី 1 ដល់ 1០០ ។ឧទាហរណ៍ ៖ បង្កើត file នៃចំនួនគត់ ៖
# include <stdio.h>
# include <conio.h>
main ( )
{ char filename[21];
int i;
long num; FILE *f1;
printf (“\n Name file:”); scanf (“%20s”, &filename);
f1 = fopen (filename, “wb”); /*open file for writing only */
/* write 100 number into file */
fwrite (&i, sizeof (int), 1, f1);
fclose(f1);
}
Note: fwrite (&i, sizeof (int), 1, f1);
- Function នេះមានសកមμភាព writedataទៅក្នុងfile(file write) មានបណ្ដា parameters
ដូចខាងក្រោម
+ &i: address of Variable i
+ ទំហំរបស់ data ដែលត្រូវ write ទៅក្នុង file
+ Address of file
+ ចុងក្រោយគឺ pointer of file
- fwrite អនុញ្ញាត្ដិអោយ write data ជាច្រើនដែលមានទំហំប៉ុនគ្នា, ទៅក្នុង file បាន ។
+ fopen ( ): អាចមានតំលៃសេμី NULL, ក្នុងករណីមានកំហុសដូចជា disk full ។
Ex: printf (“Name file:”); scanf (“%20s”, filename);
f1 = fopen (filename, “wb”);
យើងអាចជំនួសដោយ statement:
f1 = fopen (“Integer.Dat”, “wb”);
III.2) Read binary file: ដើម្បី Read ធាតុណាមួយរបស់ file ត្រូវមានលក្ខ័ខ័ណៈ តើ file នោះនៅមានធាតុសំរាប់ Read ឬអត់ ?
(pointer Æ EOF) ។ Function feof ( ) នឹងប្រាប់អោយដឹងៈ បើ Read ដល់ចុង file នោះ feof ( ) =
True ផ្ទុយទៅវិញ feof ()= False ។
វិធានដើម្បី Read file មួយយើងប្រើ ៖
ពេល Read file, ត្រូវ Read ធាតុ file ជាមុនទើបត្រួតពិនិត្យមើល តើធាតុនោះជា EOF ឬអត់ ?
if ((ch = getc (fp)) != EOF)…
ក្នុងនោះ function getc (fp) ជា function Read 1 character ឬ 1 byte របស់ file
ដែលមាន pointer fileគឺ fp។ statement នេះមានន័យថា Read 1 character ពី file fp និងពិនិត្យមើលតំលៃរបស់វាសេμី EOF ឬ អត់ ? ឬប្រើ Loop while ដើម្បីត្រួតពិនិត្យ គ្រប់ធាតុ ទាំងអស់របស់ file ដែលយើង Read . while ((ch = getc (fp)) != EOF) …
fclose(f1);
}
Note: fwrite (&i, sizeof (int), 1, f1);
- Function នេះមានសកមμភាព writedataទៅក្នុងfile(file write) មានបណ្ដា parameters
ដូចខាងក្រោម
+ &i: address of Variable i
+ ទំហំរបស់ data ដែលត្រូវ write ទៅក្នុង file
+ Address of file
+ ចុងក្រោយគឺ pointer of file
- fwrite អនុញ្ញាត្ដិអោយ write data ជាច្រើនដែលមានទំហំប៉ុនគ្នា, ទៅក្នុង file បាន ។
+ fopen ( ): អាចមានតំលៃសេμី NULL, ក្នុងករណីមានកំហុសដូចជា disk full ។
Ex: printf (“Name file:”); scanf (“%20s”, filename);
f1 = fopen (filename, “wb”);
យើងអាចជំនួសដោយ statement:
f1 = fopen (“Integer.Dat”, “wb”);
III.2) Read binary file: ដើម្បី Read ធាតុណាមួយរបស់ file ត្រូវមានលក្ខ័ខ័ណៈ តើ file នោះនៅមានធាតុសំរាប់ Read ឬអត់ ?
(pointer Æ EOF) ។ Function feof ( ) នឹងប្រាប់អោយដឹងៈ បើ Read ដល់ចុង file នោះ feof ( ) =
True ផ្ទុយទៅវិញ feof ()= False ។
វិធានដើម្បី Read file មួយយើងប្រើ ៖
ពេល Read file, ត្រូវ Read ធាតុ file ជាមុនទើបត្រួតពិនិត្យមើល តើធាតុនោះជា EOF ឬអត់ ?
if ((ch = getc (fp)) != EOF)…
ក្នុងនោះ function getc (fp) ជា function Read 1 character ឬ 1 byte របស់ file
ដែលមាន pointer fileគឺ fp។ statement នេះមានន័យថា Read 1 character ពី file fp និងពិនិត្យមើលតំលៃរបស់វាសេμី EOF ឬ អត់ ? ឬប្រើ Loop while ដើម្បីត្រួតពិនិត្យ គ្រប់ធាតុ ទាំងអស់របស់ file ដែលយើង Read . while ((ch = getc (fp)) != EOF) …
នោះនឹង Read ធាតុទាំងអស់របស់ file រហូតដល់ EOF ទើបឈប់ ។
Ex: Read file of integer number :
#include <stdio.h>
main ( )
{ char filename[21];
int i;
FILE *f1;
printf (“”\n Name file:”); scanf (“%20s”, &filename);
f1 = open (filename, “rb”);
while (fread (&i, sizeof (int), 1, f1), !feof (f1))
}
syntax:
while (Expression 1, Expression2) <Actions>;
សមមូលនឹង
do { Expression 1;
if (Expression2) <Action>;
- ឬ បើយើងដឹងចំនួនធាតុជាមុន នោះយើងសរសេរ
for ( i = 1; i <= 100; i++)
fread (&i, sizeof (int), 1, f1);
- កុំភ្លេច open file data ប្រភេទ binary “rb” ។
* ឥឡូវយើងធ្វើឧទាហរណ៍ជាក់ស្ដែង: ឧបមាថាមាន file INTEGER.Dat ផ្ទុកចំនួនពី1 ដល់1០០។ យើង Read ធាតុទី1និងទី3នៃ file ខាងលើហើយតាងអោយ Variable A,B។ Exam: Read 3ចំនួនគត់:#include<stedio.h>
#include<conio.h>
main( )
{ int A,B; long num; FILE *f1;
f1=fopen(“Integer.DAT”,”rb”); fread (&A, sizeof (int),1,f1); fread (&B, sizeof (int),1,f1); fread (&B, sizeof (int),1,f1); printf (“\n A=%d”,A);
printf (“\n B=%d”,B);
fclose (f1); getgh( ); return 0;
}
Example: Copy ពី file នៃចំនួនគត់ទៅ file ផ្សេងទៀត។ កមμវិធីសរសេរក្រោមរាង Function:
Ex: copy file of integer:
#include < stdio.h>
#include < conio.h>
FILE *f1,*f2;
printf (“Name file source:”); scanf (“%20s”,&filename1);
f1=fopen (filename1,”rb”);
prinf (“Name file destination :”); scanf (“%20s”,&filename2);
f2=fopen (filename2, “wb”);
while (fread(&i, sizeof(int),1,f1),! feof (f1))
{
fwrite (&i, sizeof(int),1,f2);
}Ex: Read file of integer number :
#include <stdio.h>
main ( )
{ char filename[21];
int i;
FILE *f1;
printf (“”\n Name file:”); scanf (“%20s”, &filename);
f1 = open (filename, “rb”);
while (fread (&i, sizeof (int), 1, f1), !feof (f1))
printf (“\n%d”, i);
fclose (f1);}
Note: Statement
while (fread(&i, sizeof(int),1, f1),!feof(f1))
printf(“\n%d”, i);
អនុវត្ដន៍ការ Read តំលៃ i, ក្រោយមកត្រួតពិិនិត្យមើ់ល EOF ហើយឬនៅ ។syntax:
while (Expression 1, Expression2) <Actions>;
សមមូលនឹង
do { Expression 1;
if (Expression2) <Action>;
} while (Expression2);
ដូច្នេះក្នុងកមμវិធីខាងលើអាចសរសេរដូចខាងក្រោម ៖
do { fread (&i, sizeof (int), 1, f1);
if (!feof (f1))
printf (“\n%d”, i);
} while (!feof (f1));- ឬ បើយើងដឹងចំនួនធាតុជាមុន នោះយើងសរសេរ
for ( i = 1; i <= 100; i++)
fread (&i, sizeof (int), 1, f1);
- កុំភ្លេច open file data ប្រភេទ binary “rb” ។
* ឥឡូវយើងធ្វើឧទាហរណ៍ជាក់ស្ដែង: ឧបមាថាមាន file INTEGER.Dat ផ្ទុកចំនួនពី1 ដល់1០០។ យើង Read ធាតុទី1និងទី3នៃ file ខាងលើហើយតាងអោយ Variable A,B។ Exam: Read 3ចំនួនគត់:#include<stedio.h>
#include<conio.h>
main( )
{ int A,B; long num; FILE *f1;
f1=fopen(“Integer.DAT”,”rb”); fread (&A, sizeof (int),1,f1); fread (&B, sizeof (int),1,f1); fread (&B, sizeof (int),1,f1); printf (“\n A=%d”,A);
printf (“\n B=%d”,B);
fclose (f1); getgh( ); return 0;
}
Example: Copy ពី file នៃចំនួនគត់ទៅ file ផ្សេងទៀត។ កមμវិធីសរសេរក្រោមរាង Function:
Ex: copy file of integer:
#include < stdio.h>
#include < conio.h>
main( )
{ char filename1 [21];
char filename2 [21];
int i;FILE *f1,*f2;
printf (“Name file source:”); scanf (“%20s”,&filename1);
f1=fopen (filename1,”rb”);
prinf (“Name file destination :”); scanf (“%20s”,&filename2);
f2=fopen (filename2, “wb”);
while (fread(&i, sizeof(int),1,f1),! feof (f1))
{
fwrite (&i, sizeof(int),1,f2);
f close (f1); f close (f2); return 0;
}
III.3/ write-read file ដែលមាន Data ប្រភេទ struct :
Example: បង្កើត file ដែលមានទិន្នន័យជា struct:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define TRUE 1
char ss[80];
typedef struct {
typedef struct {
char name[80] ;
char city[30] ;
float salary;
date dob;
} person;
person read_screen (person poeple);
FILE *fpt;
main( )
{int flag = TRUE;
person poeple;
fpt = fopen (“LIST.DAT”,”wb”);
person. salary = 0;
while (flag)
{ printf (“\n Name:”); gets (people. name);
}
fclose (fpt);
}
*/ ——————- */#include<stdio.h>
#include<conio.h>
#include<string.h>
#define TRUE 1
char ss[80];
typedef struct {
int day;
int month;
int year;
} date;typedef struct {
char name[80] ;
int homenum;
char street[80] ;char city[30] ;
float salary;
date dob;
} person;
person read_screen (person poeple);
FILE *fpt;
main( )
{int flag = TRUE;
person poeple;
fpt = fopen (“LIST.DAT”,”wb”);
person. salary = 0;
while (flag)
{ printf (“\n Name:”); gets (people. name);
if (people. name[0]= = ‘\0’)
break;
people = read_screen(people);
fwrite (&people, sizeof (person),1,fpt);}
fclose (fpt);
}
person read_screen (person people)
{ printf (“Enter day/ month/ year : ”);
scanf (“%d%d%d”, &people.dob.day, &people.dob.month,
&people.dob.year);
printf (“Home number : ”); scanf (“%d”, &people.homenum);
gets (ss); /* or while (getch ( ) ! = ‘\n’); */
printf (“City : ”);
gets (people.city);
printf (“Street : ”).
gets (people.street);
printf (“Salary :”); scanf (“%f”, &people.salary);
gets (ss);
return (people);
}
Exam: read data from file struct :
មុននឹង read file of struct យើងត្រូវដឹង struct នោះជាមុនៈ មាន field ណាខ្លះ ? ប្រភេទទិន្នន័យរបស់ field នីមួយៗ …
# include <stdio.h>
# include <conio.h>
# include <string.h>
#define TRUE 1
typedef struct { int day;
int month;
int year;
} person; void write_screen (person); FILE *fpt;
main ( )
{ person people;
while (fread (&people, sizeof(person), 1, fpt), !feof (fpt)){ printf (“Enter day/ month/ year : ”);
scanf (“%d%d%d”, &people.dob.day, &people.dob.month,
&people.dob.year);
printf (“Home number : ”); scanf (“%d”, &people.homenum);
gets (ss); /* or while (getch ( ) ! = ‘\n’); */
printf (“City : ”);
gets (people.city);
printf (“Street : ”).
gets (people.street);
printf (“Salary :”); scanf (“%f”, &people.salary);
gets (ss);
return (people);
}
Exam: read data from file struct :
មុននឹង read file of struct យើងត្រូវដឹង struct នោះជាមុនៈ មាន field ណាខ្លះ ? ប្រភេទទិន្នន័យរបស់ field នីមួយៗ …
# include <stdio.h>
# include <conio.h>
# include <string.h>
#define TRUE 1
typedef struct { int day;
int month;
int year;
} date;
typedef struct { char name[80]; int homenum; char street[80]; char city[30]; float salary; date dob;} person; void write_screen (person); FILE *fpt;
main ( )
{ person people;
fpt = fopen (“LIST.DAT”, “rb”);
{ write_screen (people);
}
fclose (fpt);
return 0;
}
void write_screen (person people)
{ printf (“Name :%s \n”, people.name);
printf (“Date of birth :%2d/ %2d/ %2d \n”, people.dob.day,people.dob.month,
people.dob.year);
printf (“Home number :%d \n”, people homenum);
printf (“Street :%s \n”, people.street);
printf (“City :%s \n”, people.city);
printf (“Salary :%8.2 f\n\n”, people.salary);
}
Note: ក្នុងឧទាហណ៍ខាងលើ, យើងគប្បីចំណាំការ Declaration និង open file ដើម្បី read ជាមួយ
“rb”, និង open file ដើម្បី write ជាមួយ “wb” ។
IV. Access ដោយផ្ទាល់ទៅធាតុរបស់ file binary :
ក្នុង C ដើម្បី access ដោយផ្ទាល់ទៅធាតុណាមួយរបស់ file យើងប្រើ fseek :
fseek (fptr, No*sizeof(one_record), SEEK_SET);
Note:
}
fclose (fpt);
return 0;
}
void write_screen (person people)
{ printf (“Name :%s \n”, people.name);
printf (“Date of birth :%2d/ %2d/ %2d \n”, people.dob.day,people.dob.month,
people.dob.year);
printf (“Home number :%d \n”, people homenum);
printf (“Street :%s \n”, people.street);
printf (“City :%s \n”, people.city);
printf (“Salary :%8.2 f\n\n”, people.salary);
}
Note: ក្នុងឧទាហណ៍ខាងលើ, យើងគប្បីចំណាំការ Declaration និង open file ដើម្បី read ជាមួយ
“rb”, និង open file ដើម្បី write ជាមួយ “wb” ។
IV. Access ដោយផ្ទាល់ទៅធាតុរបស់ file binary :
ក្នុង C ដើម្បី access ដោយផ្ទាល់ទៅធាតុណាមួយរបស់ file យើងប្រើ fseek :
fseek (fptr, No*sizeof(one_record), SEEK_SET);
Note:
+ fptr: ជា pointer file, វាអាចមានឈេμាះផ្សេងៗ …
+ No: ជាទីតាំងនៃលេខរៀងរបស់ធាតុក្នុង file ។ (ធាតុទីមួយរបស់ file មានលេខរៀង (index) សេμី 0) តែពេលយើងប្រើគឺ ចាប់ ផ្ដើមពីលេខ ១ ។
+ sizeof(onerecord), ច្រើនតែអនុវត្ដន៍ដោយ function sizeof ( ) ។ ដូច្នេះកន្សោម ៖
No *sizeof(onerecord) ;
+ parameter ចុងក្រោយក្នុង statement មាន៣ ករណីៈ
SEEK_SET (ឬ 0) រំកិលគណនាទីតាំង access ចាប់ពីដើម file
No *sizeof(onerecord) ;
+ parameter ចុងក្រោយក្នុង statement មាន៣ ករណីៈ
SEEK_SET (ឬ 0) រំកិលគណនាទីតាំង access ចាប់ពីដើម file
SEEK_CUR (ឬ 1) រំកិលគណនាពីទីតាំង current pointer file ទៅចុង file ។
SEEK_END (ឬ 2) រំកិលគណនាពីចុង file មកវិញ
SEEK_END (ឬ 2) រំកិលគណនាពីចុង file មកវិញ
* យើងនិងស្រាវជ្រាវការ Access ដោយផ្ទាល់ទៅក្នុង file
+ Read data from Keyboard និង create new
+ Read again data from file.
+ Update, data …
Ex: Access ដោយផ្ទាល់, file ដើម្បី Read : ឧបមាយើងមាន file នៃបណ្ដាចំនួនគត់ នឹងចង់
access ដោយផ្ទាល់ដើម្បី Read ធាតុទី No :
# include <stdio.h>
#include <conio.h>
main ( )
{ char filename[21];
int n;
int No;
FILE *f1;
printf (“ Name file:”); scanf (“ %20s” , filename);
f1 = fopen (filename, “ r” );
while (printf (“No :”),scantf (“ %d”,&No)
{fseek (f1, size(int) *(No-1), SEEK_SET);
fread (&n, sizeof (int), 1, f1);
printf (“ Value find is : %d \n”, n);
}
fclose(f1); getch();
}
Note: ឧទាហរណ៍ខាងលើអនុញ្ញាតិអោយយើង Read ដោយផ្ទាល់ Value នៃធាតុណាមួយក្នុង file ។
Ex: access ដោយផ្ទាល់ដើម្បី update data:
# include <stdio.h>
#include <conio.h>
main ( )
{ char filename[21];
int n;
f1 = fopen (filename,” w”);
while (printf (“No :”), scanf(“ %d”, &num),num)
+ Read again data from file.
+ Update, data …
Ex: Access ដោយផ្ទាល់, file ដើម្បី Read : ឧបមាយើងមាន file នៃបណ្ដាចំនួនគត់ នឹងចង់
access ដោយផ្ទាល់ដើម្បី Read ធាតុទី No :
# include <stdio.h>
#include <conio.h>
main ( )
{ char filename[21];
int n;
int No;
FILE *f1;
printf (“ Name file:”); scanf (“ %20s” , filename);
f1 = fopen (filename, “ r” );
while (printf (“No :”),scantf (“ %d”,&No)
{fseek (f1, size(int) *(No-1), SEEK_SET);
fread (&n, sizeof (int), 1, f1);
printf (“ Value find is : %d \n”, n);
}
fclose(f1); getch();
}
Note: ឧទាហរណ៍ខាងលើអនុញ្ញាតិអោយយើង Read ដោយផ្ទាល់ Value នៃធាតុណាមួយក្នុង file ។
Ex: access ដោយផ្ទាល់ដើម្បី update data:
# include <stdio.h>
#include <conio.h>
main ( )
{ char filename[21];
int n;
long num;
FILE *f1;
printf (“ Name file :” ); scanf (“ %20s”,filename)f1 = fopen (filename,” w”);
while (printf (“No :”), scanf(“ %d”, &num),num)
{printf (“value update:” ); scanf (“ %d”, &n); fseek (f1,sizeof
(int)*(num –1), SEEK_SET); fwrite (&n, sizeof (int), 1,f1);
}
fclose(f1);
}
Ex: access ដោយផ្ទាល់ : write និង read:
# include <stdio.h>
#include <conio.h>
int main (vod)
{ FILE *strem;
char msg[ ] = “ this is a program for example !”;
char buf[20];
if ((stream = fopen (“ DUMMY.FIL”, “w+” )) == NULL)
{ fprintf (stderr, “cannot open output file.\n”);
fseek (streem, SEEK_SET, 0);
fread (buf,strlen(msg) +1, 1, stream);
printf (“ %s\n”, buf);
fclose (stream);
return 0;
}
#include <conio.h>
# define TRUE 1
typedef struct { int day;
int month;
int year;
} date;
typedef struct { char name[80];
int homenum;
char street[80];
char city[30];
float salary;
} person;
void write_screen (person);
FILE *fpt;
main ( )
{ person people;
}
fclose(f1);
}
Ex: access ដោយផ្ទាល់ : write និង read:
# include <stdio.h>
#include <conio.h>
int main (vod)
{ FILE *strem;
char msg[ ] = “ this is a program for example !”;
char buf[20];
if ((stream = fopen (“ DUMMY.FIL”, “w+” )) == NULL)
{ fprintf (stderr, “cannot open output file.\n”);
return 1;
}
fwrite (msg,strlen (msg) +1, 1, stream);fseek (streem, SEEK_SET, 0);
fread (buf,strlen(msg) +1, 1, stream);
printf (“ %s\n”, buf);
fclose (stream);
return 0;
}
Ex: update file struct ខាងលើៈ (file LIST.DAT) :
# include <stdio.h>#include <conio.h>
# define TRUE 1
typedef struct { int day;
int month;
int year;
} date;
typedef struct { char name[80];
int homenum;
char street[80];
char city[30];
float salary;
} person;
void write_screen (person);
FILE *fpt;
main ( )
{ person people;
int No;
long size, pos;
fpt = fopen (“LIST.DAT”, “rb”);
fseek (fpt, 0, SEEK_END);
size = ftell(fpt);
printf (“No :”); scanf (“%d”, &No);
pos = No *sizeof (person);
if (No < 0 || pos > size)
printf (“\nHave no record in file”);else
{ fseek (fpt, pos –1, 0);
fread (&people, sizeof (person), 1, fpt);
write_screen (people);
}
}
void write_screen (person people) {dUcxagelI}
IV.1. Text Files :
បណ្ដាធាតុរបស់ Text file គឺជាបណ្ដា characters, string, text ។
បណ្ដា Function write ទៅក្នុង text file :
putc (ch, fptr); write 1 character ទៅក្នុង text file
fputs (str, fptr); write string ទៅក្នុង text file
fprintf (fptr,…); សមមូល printf ( )
Ex: write string នៃបណ្ដា អក្សរធំទៅក្នុង file:
កមμវិធីនេះ Read 1 បន្ទាត់ character ពី keyboard និង write ទៅក្នុង text file:
# include <stdio.h>
main ( )
{ FILE *fpt;
char c;
fpt = fopen (“c:\\tc\\sample.Dat”, “w”);
do { putc (toupper (c = getchar ( ) ), fpt);
} while (c! = ‘\n’);
fclose (fpt);}
ª ចំណាំ នេះជា Function គណនា text file. ។ Statement putc ប្រាប់អោយយើងដឹងថានេះជា
file text ។ ជា statement write 1 character ទៅក្នុង text file ។
ªStatement openfile :
fpt = fopen (“c:\\tc\\sample.dat”, “w”);IV.2.Read data from text file : ដើម្បីបើក file text :
fopen (…, “r”);
ចំណាំ ៖ “r” មានន័យថា Read only
Read data from text file :
ការ Read បណ្ដាធាតុពី file ក្រោយពីបើក file ហើយ ៖
getc (fptr); Read charater form text filefgets (str, fptr); Read string str ពី text file fptr ។
fscanf ( ); Read បណ្ដា Value ពី text file, សមមូល scanf ( );
# include <stdio.h>
main ( )
{ FILE *fpt;
char c;
if ((fpt = fopen (“C:\\TC\\sample.DAT”, “r”)) == NULL)
printf (“\a\n Erro, Cannot open file”)
else
do { putchar (c = getc (fpt));
} while (c! = ‘\n’);fclose (fpt);
}

IV.3. លំហាត់អនុវត្ដ ៖
ឧទាហរណ៍ខាងក្រោមអធិប្បាយពីការ write និង Read បណ្ដា Record ទៅក្នុង text file ។# include <stdio.h>
# include <conio.h>
# include <string.h>
# define TRUE 1
typedef struct { int day;
int month;
int year;} date;
typedef struct { char name[80]; int homenum; char street[80]; char city[30]; float salary; date dob;
} person;
person read_screen (person people); void write_file (person people); FILE *fpt;
char ss[80];
main ( )
{ person people;
int flag = TRUE;
fpt = fopen (“LIST.DAT”, “w”);
people.salary = 0;
while (flag)
{ printf (“\n Name:”); gets (people.name);
if (people.name[0] == ‘\0’)break;
people = read_screen (people);
write_file (people);
}
fclose (fpt);
}
person read_screen (person people)
{ printf (“Enter day /month/ year:”);scanf (“%d%d%d”, &people.dob.day, &people.dob.month,
&people.dob.year) ;
printf (“home number :”);scanf (“%d”, &people.homenum);
gets (ss); /* or while (getchar ( ) != ‘\n’);
printf (“street:”); gets (people.street); printf (“city:”); gets (people.city); printf (“salary:”);
scanf (“%f”, &people.salary);
gets (ss);
void write_file (person people).
{ fprintf (fpt, “%d%d%d\n”, people.dob.day, people.dob.month,people.dob.year);
fprintf (fpt, “%d\n”, people.homneum);
fprintf (fpt, “%s\n”, people.street);
fprintf (fpt, “%s\n”, people.city);
fprintf (fpt, “%f\n”, people.salary);
}
IV.4. បណ្ដាឧទាហរណ៍គណនា text :
ក្នុងផ្នែកនេះយើងសិក្សាពីបណ្ដាឧទាហរណ៍គណនា file text ដូចជា copy file, រាប់ចំនួនបន្ទាត់ទៅក្នុង
text file :
Ex: រាប់ចំនួនបន្ទាត់ទៅក្នុង text file:# define EOF –1
# define EOL 0x0A /* or ‘\n’ */
int ch, count;
…
ch = getch ( ); count = 0;
while (ch != EOF)
{ while (ch != EOL) ch = getchar ( );
count ++;
ch = gatchar ( );
}
ឬសរសេរម្យ៉ាងទៀតខ្លីជាង ៖
# define EOF –1# define EOL 0x0A /* or ‘\n’ */
int ch, count = 0;
…
ch = getchar ( );
while ((ch = getchar ( )) != EOF)
while ((ch = getchar ( )) != EOL)
cout ++;
Ex: រាប់ចំនួន character ឬ គណនាទំហំរបស ់ file :
ចូររាប់មើលក្នុង file មួយមានផ្ទុកប៉ុនμាន character ?
# include <stdio.h>
# include <conio.h>
# include < stdlib.h>
main (int argc, char *argv[ ])
{ int ch;
char filename[20]; FILE *fp;
long count = 0;
printf (“\n Name file :”); scanf (“%s”,&filename);
fp = fopen (filename, “r”);
while ((ch = getc(fp)) != EOF)
{ putc (ch, stdout); /*Display character on screen);
count ++;
printf (“file %s have got %ld character.”, filename, count);
return 0;
}
Note:
+ កមμវិធីខាងលើអនុវត្ដន៍តែចំពោះ text file ដែលផ្ទុក character ក្នុងតារាង code ASCII ។
+ ចំនួន character ដែល Display on screen តិចជាងចំនួន byte ជាក់ស្ដែងរបស់ file ។ ព្រោះ
Function getc ( ) មិនរាប់ character, ដើមបន្ទាត់ ‘\r’ ។ function getc ( ) ចាត់ទុក character
‘\r’ និង ‘\n’ តែមួយ character ប៉ុណ្ណោះ ។
ដូច្នេះដើម្បីរាប់អោយត្រឹមត្រូវចំនួន byte ក្នុង file, នោះយើងត្រូវ openfile ដើម្បី Read ជា “rb”:
if ((fp = fopen (argv[1], “rb”)) == NULL)
ពេលនោះ function getc ( ) នឹងមិនបែងចែក character ទៀតវាគណនាគ្រប់ character
ទាំងអស់និងរាប់ ចំនួន byte ទាំងអស់ក្នុង file ។
VI. បណ្ដា function គណនា file របស់ turbo C:
frewind( ): នាំ cursor file ទៅទីតាំងនៃធាតុដំបូងរបស់ file
Ex: ខាងក្រោមនឹង Display ឡើងវិញ អត្ថន័យរបស់ file ដែលទើបនឹងបង្កើតឡើង ។
ដើម្បីធ្វើការងារនេះ ដំបូងត្រូវនាំ cursor file ទៅទីតាំងនៃធាតុតំបូង, បន្ទាប់មកប្រើ function fgets ( )
ដើម្បី Read ឡើងវិញបណ្ដា តំលៃរបស់ file ។
# include <stdio.h>
# include <stdlib.h>
# include < conio.h>
# include <string.h>
main ( )
{ char str[80];
FILE *fp;
if ((fp = fopen (“TEST”, “w+”)) == NULL)
}
do { printf (“Enter string:”); gets(str);
strcat (str, ‘\n’); /* add new line */
count ++;
}
fclose (fp);printf (“file %s have got %ld character.”, filename, count);
return 0;
}
Note:
+ កមμវិធីខាងលើអនុវត្ដន៍តែចំពោះ text file ដែលផ្ទុក character ក្នុងតារាង code ASCII ។
+ ចំនួន character ដែល Display on screen តិចជាងចំនួន byte ជាក់ស្ដែងរបស់ file ។ ព្រោះ
Function getc ( ) មិនរាប់ character, ដើមបន្ទាត់ ‘\r’ ។ function getc ( ) ចាត់ទុក character
‘\r’ និង ‘\n’ តែមួយ character ប៉ុណ្ណោះ ។
ដូច្នេះដើម្បីរាប់អោយត្រឹមត្រូវចំនួន byte ក្នុង file, នោះយើងត្រូវ openfile ដើម្បី Read ជា “rb”:
if ((fp = fopen (argv[1], “rb”)) == NULL)
ពេលនោះ function getc ( ) នឹងមិនបែងចែក character ទៀតវាគណនាគ្រប់ character
ទាំងអស់និងរាប់ ចំនួន byte ទាំងអស់ក្នុង file ។
VI. បណ្ដា function គណនា file របស់ turbo C:
frewind( ): នាំ cursor file ទៅទីតាំងនៃធាតុដំបូងរបស់ file
Ex: ខាងក្រោមនឹង Display ឡើងវិញ អត្ថន័យរបស់ file ដែលទើបនឹងបង្កើតឡើង ។
ដើម្បីធ្វើការងារនេះ ដំបូងត្រូវនាំ cursor file ទៅទីតាំងនៃធាតុតំបូង, បន្ទាប់មកប្រើ function fgets ( )
ដើម្បី Read ឡើងវិញបណ្ដា តំលៃរបស់ file ។
# include <stdio.h>
# include <stdlib.h>
# include < conio.h>
# include <string.h>
main ( )
{ char str[80];
FILE *fp;
if ((fp = fopen (“TEST”, “w+”)) == NULL)
{ printf (“Cannot open file .\n”);
exit (1);}
do { printf (“Enter string:”); gets(str);
strcat (str, ‘\n’); /* add new line */
fputs (str, fp);
} while (*str != ‘\n’);
/* Now Read And Display Data of file */
frewind (fp);
while (!feof (fp))
{ fgets (str, 79, fp);
printf (str);
}
getch ( );
return 0;
}return 0;
+ ferror ( ):
Function ferror ( ) កំណត់ការគណនា file មានកំហុស ឬអត់ ?
- ផ្ដល់តំលៃ TRUE បើមាន error
- ផ្ដល់តំលៃ FALSE បើគμាន error
យើងគប្បីប្រើ ferror ( ) ក្រោយពី statement គណនា file :
- ch = getc (fp);
if (ferror (fp))…
- putc (ch,fp);
if (ferror(fp))…
+ remove ( ):
Function remove ( ) នឹង delete file ចេញពី disk ។
fflush (stdin) ;
+ fflush ( ): ប្រើសំរាប់ clear stdin ។
VII. បញ្ជូន parameter ជា file ទៅអោយ function :
Ex: copy file ពីរៈ
# include <stdio.h>
main (int argc, char *argv[ ])
{ FILE *fp;
void filecopy (FILE *, FILE *);
if (argc == 1)
filecopy (stdin, stdout);
else
while ( — argc > 0)
if ((fp = open (* ++ argv, “r”)) == NULL)
{ printf (“Cannot open %s \n”, *argv);
fclose (fp);
}
return 0;
}
void filecopy (FILE *fpi, FILE *fpo)
{ int c;
while ((c = getc (fpi)) != EOF)
}
# include <stdlib.h>
main (int argc, char *argv[ ])
{ FILE *in, *out;
if ((in = fopen (argv[1], “rb”)) == NULL)
{ printf (“Cannot open source file \n”);
exit (1);
}
if ((out = fopen (argv[2], “wb”)) == NULL)
{ printf (“cannot open destination file.\n”);
exit (1);
}VII. បញ្ជូន parameter ជា file ទៅអោយ function :
Ex: copy file ពីរៈ
# include <stdio.h>
main (int argc, char *argv[ ])
{ FILE *fp;
void filecopy (FILE *, FILE *);
if (argc == 1)
filecopy (stdin, stdout);
else
while ( — argc > 0)
if ((fp = open (* ++ argv, “r”)) == NULL)
{ printf (“Cannot open %s \n”, *argv);
return 1;
} else { filecopy (fp, stdout);fclose (fp);
}
return 0;
}
void filecopy (FILE *fpi, FILE *fpo)
{ int c;
while ((c = getc (fpi)) != EOF)
}
យើងអាចសរសេរដោយរបៀបម្យ៉ាងទៀតគឺ ៖
# include <stdio.h># include <stdlib.h>
main (int argc, char *argv[ ])
{ FILE *in, *out;
char ch;
if (argc != 3) { printf (“\n fore got namefile:”);
exit (1);
}if ((in = fopen (argv[1], “rb”)) == NULL)
{ printf (“Cannot open source file \n”);
exit (1);
}
if ((out = fopen (argv[2], “wb”)) == NULL)
{ printf (“cannot open destination file.\n”);
exit (1);
while (!feof (in)) { ch = getc (in);
if (!feof (in)) putc(ch,out);
}
fclose (in);
fclose (out); gatch ( );
return 0;
}
# include <stdio.h>
# include <stdlib.h>
main (int argc, char *argv[ ])
{ int ch;
FILE *fp;
long count = 0;
if (argc != 2) { printf (“use :%s name file, argv[0]);
exit (1);
}
if ((fp = fopen (argv[1], “r”)) == NULL)
{ printf (“Cannot open %s”, argv[1]);
exit (1);
}
while ((ch = getc(fp) != EOF)
{ putc (ch, stdout);
count ++;
}
fclose (fp);
printf (“file% have %ld charaacter”, argv[1], count);
return 0;
}
if (!feof (in)) putc(ch,out);
}
fclose (in);
fclose (out); gatch ( );
return 0;
}
# include <stdio.h>
# include <stdlib.h>
main (int argc, char *argv[ ])
{ int ch;
FILE *fp;
long count = 0;
if (argc != 2) { printf (“use :%s name file, argv[0]);
exit (1);
}
if ((fp = fopen (argv[1], “r”)) == NULL)
{ printf (“Cannot open %s”, argv[1]);
exit (1);
}
while ((ch = getc(fp) != EOF)
{ putc (ch, stdout);
count ++;
}
fclose (fp);
printf (“file% have %ld charaacter”, argv[1], count);
return 0;
}
សំនួរ
1). តើអាចប្រើ Function gets (fp,…) ដើម្បី Read បណ្ដាចំនួនគត់ក្នុង file F: int *F ?2). អាចប្រើកមμវិធី text Editor ដើម្បីមើលអត្ថន័យបណ្ដា file int *F, float *F មានឬអត់ ? ព្រោះអ្វី?
3). command TYPE, PRINT របស់ Dos អនុវត្ដន៍ជាមួយ file ប្រភេទណា ?
លំហាត់
1). សរសេរកមμវិធី create file F3, ផ្ទុកបណ្ដាចំនួនគត់, ដោយរបៀបភ្ជាប់ 2 file នៃចំនួនគត់ F1 និង F2ជាមួយគ្នា ។2). សរសេរកមμវិធីរាប់ចំនួន character ក្នុង text File មួយឈេμាះ F ។
3). គូសឬសរសេរកមμវិធីបង្កើត file នៃចំនួនមួយ, ដោយ Read បណ្ដាចំនួនគត់ពី text File មួយទៀត ។
បន្ទាត់ នីមួយៗរបស់ text file អាចមានផ្ទុក ឬគμានផ្ទុក ចំនួនគត់, ឬមានផ្ទុកបណ្ដាចំនួនគត់មួយចំនួន ។
5). សរសេរកមμវិធីបង្កើត text file ផ្ទុកឈេμាះ, អាយុ, អាស្រ័យដ្ឋាន របស់អ្នក ។ ក្រោយមកកមμវិធីនឹង
Read file នេះឡើងវិញដើម្បី print ទៅ printer អត្ថន័យខាងលើ (ដោយមនុស្សម្នាក់នៅលើបន្ទាត់តែ១)។
6). ក្នុងពេល open file ថμីយើងកំពុង open, នោះ file ចាស់ត្រូវបានលប់ចោល ។
ចូររកវិធានណាដើម្បីបើជួប ករណី នេះអោយវាប្រាប់យើងថា,fileនេះមានស្រាប់នៅលើ Disk ហើយ,អ្នកក៏ចង់លុបវាចោល ដើម្បីបង្កើត ថμី ឬអត់ ?
7). សរសេរកមμវិធីគណនាចំនួនបន្ទាត់ដែលមានក្នុង text file មួយ ។
8). សរសេរកមμវិធី Display បណ្ដា file ទាំងអស់ ។ ក្រោយ មក យើងអាចលុប file ណាមួយដែលយើងចង់លុប ។
9). សរសេរកមμវិធីប្ដូរឈេμាះ file ។ មានន័យថា ត្រូវសរសេរ rename មាន Read parameter ។
10). ឧបមាមាន file text, ដែលបណ្ដាបន្ទាត់នីមួយផ្ទុកពី ១ដល់ ១០០ character ចូរសរសេរកមμវិធីបង្កើត
file ថμីមួយដែលបណ្ដាបន្ទាត់របស់វាផ្ទុកត្រូវ 100 character, បណ្ដា character Read from keyboard
Post a Comment