You are on page 1of 55

TRUNG TM TIN HC

I HC KHOA HC T NHIN-TP.HCM

K THUT LP TRNH C

TRUNG TM TIN HC
I HC KHOA HC T NHIN-TP.HCM

Chng 1: Tp tin
1. 2. 3. 4. 5. 6. Khi nim tp tin Phn loi tp tin Cc bc x l tp tin Con tr tp tin Vng m Cc hm thao tc tp tin
2

Chng 1: Tp tin

1. Khi nim tp tin


Trong cc chng trnh trc th cc d liu a vo chng trnh ch c tn ti trong b nh RAM, khi thot chng trnh th tt c d liu u b mt. khc phc tnh trng ny C cung cp cho ta cc hm lu tr v truy xut tp tin, l kiu FILE .

Chng 1: Tp tin

2. Phn loi tp tin


a. Tp tin vn bn (text file) Ni dung tp tin c lu tr di dng vn bn gm nhiu dng. Hai dng lin tip nhau c ngn cch bi k hiu ngn cch dng (gm hai k t \r (m 13) v \n (m 10)). V d: Cc tp tin: *.cpp, *.txt, ... l cc tp tin vn bn.
4

Chng 1: Tp tin

b. Tp tin nh phn (binary file) Ni dung tp tin c lu tr theo mt cu trc ring. Mi tp tin nh phn do mt phn mm no to ra th s c cu trc do phn mm quy nh. V d: Cc tp tin: *.exe, *.doc, *.xls, *.bmp, ... l cc tp tin nh phn

Chng 1: Tp tin

3. Cc bc x l tp tin
Bc 1: M tp tin chun b lm vic. Bc 2: Thc hin thao tc c/ghi tp tin: c d liu t tp tin vo bin nh hoc ghi d liu t bin nh vo tp tin. Bc 3: ng tp tin.

Chng 1: Tp tin

4. Con tr tp tin
Khi m tp tin chun b lm vic, ty theo cch thc m tp tin mt con tr tp tin s c t ti vi tr u hoc cui tp tin. Mi thao tc c/ghi tp tin s tc ng ln v tr hin hnh ca con tr tp tin. Sau khi thc hin xong mt thao tc c/ghi con tr tp tin s t ng di i mt s byte ng bng s byte c/ghi.
7

Chng 1: Tp tin

5. Vng m
Trong C, tp tin c gn lin vi mt vng m. Mi thao tc c/ghi tp tin thng c tin hnh trn vng m ch khng hn trn tp tin. Khi ghi, d liu t bin nh c a vo vng m v khi no vng m y th vng m mi c y ln a. Khi c, d liu c ly t vng m a vo bin nh v ch khi no vng m trng rng th my mi ly d liu t a a vo vng m.
8

Chng 1: Tp tin

6. Cc hm thao tc tp tin
a. Cc hm ng m tp tin FILE *fopen(char *tentaptin, char *kiu); Hm m tp tin chun b lm vic. Nu thnh cng hm tr v mt con tr kiu FILE ng vi tp tin va m, nu tht bi hm tr v gi tr NULL. Kiu c th nhn cc gi tr sau:
9

Chng 1: Tp tin

rt: M mt tp tin vn bn c. Tp tin cn tn ti nu khng s c li. wt: M mt tp tin vn bn mi ghi. Nu tp tin tn ti n s b xa. at: M mt tp tin vn bn ghi thm. Nu tp tin cha tn ti th s to tp tin mi. r+t : M mt tp tin vn bn c/ghi. Tp tin cn tn ti nu khng s c li.
10

Chng 1: Tp tin

w+t: M mt tp tin vn bn mi c/ghi. Nu tp tin tn ti n s b xa. a+t: M mt tp tin vn bn c/ghi thm. Nu tp tin cha tn ti th s to tp tin mi. rb: M mt tp tin nh phn c. Tp tin cn tn ti nu khng s c li. wb: M mt tp tin nh phn mi ghi. Nu tp tin tn ti n s b xa.

11

Chng 1: Tp tin

ab: M mt tp tin nh phn ghi thm. Nu tp tin cha tn ti th s to tp tin mi. r+b : M mt tp tin nh phn c/ghi. Tp tin cn tn ti nu khng s c li. w+b: M mt tp tin nh phn mi c/ghi. Nu tp tin tn ti n s b xa. a+b: M mt tp tin nh phn c/ghi thm. Nu tp tin cha tn ti th s to tp tin mi
12

Chng 1: Tp tin

int fclose(FILE *fp); Hm dng ng tp tin fp. Nu thnh cng hm tr v gi tr 0, nu tht bi hm tr v gi tr EOF (s -1).

13

Chng 1: Tp tin

int feof(FILE *fp); Hm kim tra cui tp tin fp. Nu gp cui tp tin khi c hm tr v gi tr khc 0, ngc li hm tr v gi tri 0. int fflush(FILE *fp); Hm dng lm sch vng m ca tp tin fp. Nu thnh cng hm tr v gi tr 0, ngc li hm tr v gi tr EOF.

14

Chng 1: Tp tin

b. Cc hm c/ghi tp tin vn bn int fprintf(FILE *fp, chui nh dng [, danh sch cc tham s]); Gi tr ca cc tham s c ghi ln tp tin fp theo khun dng xc nh trong chui nh dng. Nu thnh cng hm tr v mt gi tr nguyn bng s byte ghi ln tp tin, nu tht bi hm tr v gi tr EOF.

15

Chng 1: Tp tin

int fscanf(FILE *fp, chui nh dng [,danh sch cc tham s]); c d liu t fp, bin i theo khun dng trong chui nh dng v lu kt qu vo danh sch cc tham s. Hm tr v mt gi tr bng s trng c c.

16

Chng 1: Tp tin

int fputs(char *s, FILE *fp); Ghi chui s ln tp tin fp (k t \0 khng ghi ln tp tin). Nu thnh cng hm tr v m k t cui cng ghi ln tp tin, nu tht bi hm tr v gi tri EOF.

17

Chng 1: Tp tin

char *fgets(char *s, int n, FILE *fp); c mt dy k t t tp tin fp v lu vo chui s. Vic c kt thc khi hoc c c n-1 k t hoc gp du xung dng hoc kt thc tp tin. Nu thnh cng hm tr v con tr ti chui s, nu tht bi hoc gp cui tp tin hm tr v gi tr NULL.

18

Chng 1: Tp tin

V d 1: To mt tp tin vn bn c tn l Tho.txt nm trong th mc hin hnh c ni dung nh sau: Qu hng l chm kh ngt Cho con tro hi mi ngy
int TaoTT(char *tentt); void main() { int kq = TaoTT(Tho.txt); if(kq ==0) printf(Khng to c tp tin\n); }

19

Chng 1: Tp tin

int TaoTT(char *tentt) { FILE *fp; int ret; fp = fopen(tentt, wt); if(fp != NULL) { fprintf(fp, Qu hng \n); fprintf(fp, Cho con \n); fclose(fp); ret = 1; //Thnh cng } else ret = 0; //Tht bi return ret; }

20

Chng 1: Tp tin

V d 2: c tng dng ca tp tin vn bn Tho.txt mi va to trn v xut ra mn hinh.


#define MAX_LEN 256 int DocTT(char *tentt); void main() { int kq = DocTT(Tho.txt); if(kq ==0) printf(Tp tin khng c trn a);

21

Chng 1: Tp tin

int DocTT(char *tentt) { FILE *fp; char s[MAX_LEN]; int ret; fp = fopen(tentt, rt); if(fp != NULL) { while(!feof(fp)) { if(fgets(s, MAX_LEN, fp) != NULL) printf(%s, s); } fclose(fp); ret = 1; } else ret = 0; return ret; }
22

Chng 1: Tp tin

V d 3: Ghi thm vo tp tin vn bn Tho.txt mi va to trn hai cu th c nhp t bn phm c ni dung sau: Qu hng l ng i hc Con v rp bm vng bay
void main() { int kq = GhiThemTT(Tho.txt); if(kq == 1) { printf(** Noi dung tap tin sau khi ghi them **\n); DocTT(Tho.txt); } else printf(Tp tin khng c trn a\n); }
23

Chng 1: Tp tin

int GhiThemTT(char *tentt) { FILE *fp; int ret, i; char s[MAX_LEN]; fp = fopen(tentt, at); if(fp != NULL) { printf(Nhp d liu mun thm:\n); for(i = 0; i < 2; i++) { gets(s); fprintf(fp, %s\r\n, s); } fclose(fp); ret = 1; } else ret = 0; return ret; }
24

Chng 1: Tp tin

V d 4: Nhp vo tn hai tp tin vn bn. Hy ghp ni dung tp tin th hai vo tp tin th nht.
void main() { char tentt1[MAX_LEN], tentt2[MAX_LEN]; printf(Nhap ten tap tin thu 1:); gets(tentt1); printf(Nhap ten tap tin thu 2:); gets(tentt2); int kq = GhepTT(tentt1, tentt2);

25

Chng 1: Tp tin

if(kq == -2) printf(Tap tin %s v %s khong co tren dia\n, tentt1, tentt2); else if(kq == -1) printf(Tap tin %s khong co tren dia\n, tentt1); else if(kq == 0) printf(Tap tin %s khong co tren dia\n, tentt2); else { printf(Tap tin %s sau khi ghep\n, tentt1); DocTT(tentt1); } }
26

Chng 1: Tp tin

int GhepTT(char *tentt1, char *tentt2) { FILE* fp1, *fp2; char s[MAX_LEN]; int ret; fp1 = fopen(tentt1, at); fp2 = fopen(tentt2, rt); if(fp1 == NULL && fp2 == NULL) ret = -2; else if(fp1 == NULL) { ret = -1; if(fp2 != NULL) fclose(fp2); } else if(fp2 == NULL) { ret = 0; fclose(fp1); } else { while(!feof(fp2)) { if(fgets(s, MAX_LEN, fp2) != NULL) fprintf(fp1, %s, s); } fclose(fp1); fclose(fp2); ret = 1; } return ret; }
27

Chng 1: Tp tin

V d 5: Nhp mt ma trn t mt tp tin vn bn c tn MaTran.txt nm trong th mc hin hnh, tp tin c ni dung nh sau: Dng u tp tin lu gi tri sd v sc (s dng v s ct ca ma trn), hai s ny ngn cch nhau bi khong trng. Cc dng tip theo, mi dng tp tin lu mt dng ca ma trn, hai s k nhau ngn cch nhau bi khong trng.
28

Chng 1: Tp tin

#define SIZE1 5 #define SIZE2 6 struct MaTran{ int sd, sc; int arr[SIZE1][SIZE2]; }; void main() { MaTran mt; int kq = DocMT(MaTran.txt, &mt); if(kq == 1) { printf(** Ma tran sau khi doc **\n); XuatMT(mt); } else printf(Tap tin MaTran.txt khong co tren dia\n); }
29

Chng 1: Tp tin

int DocMT(char *tentt, MaTran *u) { FILE *fp; int i, j, tam, ret; fp = fopen(tentt, rt); if(fp != NULL) { fscanf(fp, %d, &(u->sd)); fscanf(fp, %d, &(u->sc)); if(u->sd > SIZE1) u->sd = SIZE1; if(u->sc > SIZE2) u->sc = SIZE2;

30

Chng 1: Tp tin

for(i = 0 ; i < u->sd ; i++) for(j = 0 ; j < u->sc ; j++) { if(!feof(fp)) { fscanf(fp, %d, &tam); u->arr[i][j] = tam; } else u->arr[i][j] = 0; } fclose(fp); ret = 1; } else ret = 0; return ret; }
31

Chng 1: Tp tin

void XuatMT(MaTran u) { int i, j; printf(So dong: %d\n, u.sd); printf(So cot: %d\n, u.sc); for(i = 0 ; i < u.sd ; i++) { for(j = 0 ; j < u.sc ; j++) printf(%d\t, u.arr[i][j]); printf(\n); } }

32

Chng 1: Tp tin

c. Cc hm c/ghi tp tin nh phn int fwrite(void *ptr, int size, int n, FILE *fp); Ghi n phn t, mi phn t c kch thc l size byte, t vng nh c tr bi con tr ptr ln tp tin fp. Hm tr v mt gi tr bng s phn t thc s ghi c.

33

Chng 1: Tp tin

int fread(void *ptr, int size, int n, FILE *fp); c n phn t, mi phn t c kch thc l size byte, t tp tin fp v lu vo vng nh c tr bi con tr ptr. Hm tr v mt gi tr bng s phn t thc s c c. void rewind(FILE *fp); Di chuyn con tr tp tin v u tp tin fp. Khi vic truy xut trn tp tin c thc hin t u tp tin.
34

Chng 1: Tp tin

long ftell(FILE *fp); Nu thnh cng hm tr v v tr hin ti ca con tr tp tin (byte th my trn tp tin, s th t byte c tnh t 0), Nu c li hm tr v gi tr -1L.

35

Chng 1: Tp tin

int fseek(FILE *fp, long sb, int xp); Di chuyn con tr tp tin xut pht t v tr xp qua mt s byte bng gi tr tuyt i ca sb. Chiu di chuyn l v cui tp tin nu sb dng, v u tp tin nu sb m. Nu thnh cng hm tr v gi tr 0, nu tht bi hm tr v gi tr khc 0.

36

Chng 1: Tp tin

xp nhn mt trong 3 gi tr sau: 0 (hoc SEEK_SET): xut pht t u tp tin. 1(hoc SEEK_CUR): xut pht t v tr hin hnh ca con tr tp tin. 2(hoc SEEK_END): xut pht t cui tp tin.

37

Chng 1: Tp tin

V d 1: To mt tp tin cha danh sch cc sinh vin, danh sch ny c lu sn trong mt mng cu trc.
#define SIZE 20 struct SINHVIEN { char ma[10]; char ten[30]; int namsinh; float diem; }; struct DSSV{ int n; SINHVIEN arr[SIZE]; };
38

Chng 1: Tp tin

int TaoTTDSSV(char *tentt, DSSV u) { FILE *fp; int ret; fp = fopen(tentt, wb); if(fp != NULL) { /*Ghi u.n cu trc, mi cu trc kch thc sizeof(SINHVIEN ), cha trong mng u.arr vo tp tin fp*/ fwrite(u.arr, sizeof(SINHVIEN), u.n, fp); fclose(fp); ret = 1; } else ret = 0; return ret; }
39

Chng 1: Tp tin

void main() { DSSV dssv; char tentt[MAX_LEN]; printf(Nhap ten tap tin muon to:); gets(tentt); NhapDSSV(&dssv); //Xem li chng cu trc int kq = TaoTTDSSV(tentt, dssv); if(kq == 0) printf(Khong the tao tap tin\n); }

40

Chng 1: Tp tin

V d 2: c danh sch cc sinh vin cha trong tp tin va to trn v xut ra mn hnh.
int DocTTDSSV(char *tentt); void main() { char tentt[MAX_LEN]; <Nhap ten tap tin muon doc> int kq = DocTTDSSV(tentt); if(kq == 0) printf(Tap tin %s khong ton tai tren dia\n, tentt); }
41

Chng 1: Tp tin

int DocTTDSSV(char *tentt) { FILE *fp; SINHVIEN sv; int ret; fp = fopen(tentt, rb); if(fp != NULL) { while(!feof(fp)) { /*c mt cu trc c kch thc sizeof(SINHVIEN) t tp tin fp vo bin sv*/ if(fread(&sv, sizeof(SINHVIEN), 1, fp) == 1) Xuat1SV(sv); } fclose(fp); ret = 1; } else ret = 0; return ret; }
42

Chng 1: Tp tin

V d 3: Tng t nh v d trn nhng danh sch cc sinh vin c t tp tin c lu vo mt mng cu trc (s sinh vin c c khng c vt qu kch thc mng cu trc), sau xut mng cu trc ny ra mn hnh.

43

Chng 1: Tp tin

int DocTTDSSV2(char *tentt, DSSV *u); void main() { char tentt[MAX_LEN]; DSSV dssv; <Nhap ten tap tin muon doc> int kq = DocTTDSSV2(tentt, &dssv); if(kq == 1) XuatDSSV(dssv); //Xem li chng cu trc else printf(Tap tin %s khong co tren dia\n, tentt); }

44

Chng 1: Tp tin

int DocTTDSSV2(char *tentt, DSSV *u) { FILE *fp; int ret; fp = fopen(tentt, rb); if(fp != NULL) { u->n = 0; while(!feof(fp) && u -> n < SIZE) { if(fread(&u->arr[u->n], sizeof(SinhVien), 1, fp) == 1) u->n++; } fclose(fp) ret = 1; } else ret = 0; return ret; }
45

Chng 1: Tp tin

V d 4: Thm mt sinh vin vo tp tin cha danh sch sinh vin to trn.
int ThemSVVaoTTDSSV(char *tentt, SINHVIEN sv) { FILE *fp; int ret; fp = fopen(tentt, ab); if(fp != NULL) { /*Ghi cu trc c kch thc sizeof(SINHVIEN ) cha trong bin sv ln tp tin fp*/ fwrite(&sv, sizeof(SINHVIEN), 1, fp); fclose(fp); ret = 1; } else ret = 0; return ret; }
46

Chng 1: Tp tin

void main() { SINHVIEN sv; char tentt[MAX_LEN]; printf(Nhap ten tap tin muon them:); gets(tentt); printf(Nhap thong tin sinh vien muon them\n); Nhap1SV(&sv); //Xem li chng cu trc int kq = ThemSVVaoTTDSSV (tentt, sv); if(kq == 1) { printf(Danh sach sinh vien sau khi them\n); DocTTDSSV(tentt); } else printf(Tap tin %s khong ton tai tren dia\n, tentt); }
47

Chng 1: Tp tin

V d 5: Nhp vo tn tp tin cha danh sch sinh vin, m sinh vin v nm sinh mi. Hy tm trong tp tin mt sinh vin c m trng vi m nhp vo, nu tm thy th sa nm sinh c thnh nm sinh mi.

48

Chng 1: Tp tin

void main() { char ma[10]; int namsinh; char tentt[MAX_LEN]; <Nhap ten tt muon sua> < Nhap ma sinh vien cua sinh vien can sua> <Nhap nam sinh moi> int kq = SuaTTDSSV(tentt, ma, namsinh); if(kq == 0) printf(Tap tin %s khong co tren dia\n, tentt); else if(kq == -1) printf(Khong tim thay sv co ma %s\n, ma); else { printf(** Danh sach sinh vien moi **\n); DocTTDSSV(tentt); } }
49

Chng 1: Tp tin

int SuaTTDSSV(char *tentt, char *ma, int namsinh) { FILE *fp ; SINHVIEN sv; int timthay, vitri, ret; fp = fopen(tentt, r+b); if(fp != NULL) { timthay = 0; while(!feof(fp)) { vitri = ftell(fp); if(fread(&sv, sizeof(SINHVIEN), 1, fp) == 1) { if(strcmp(sv.ma, ma) == 0) { sv.namsinh = namsinh; fseek(fp, vitri, SEEK_SET); fwrite(&sv, sizeof(SINHVIEN), 1, fp); timthay = 1; break; }}} fclose(fp); ret = (timthay == 1 ? 1:-1);} else ret = 0; return ret; }
50

Chng 1: Tp tin

d. Cc hm c/ghi tp tin dng chung cho c tp tin vn bn v nh phn int fputc(int ch, FILE *fp); Hm ghi ln tp tin mt k t c m l ch%256 trong ch c xem l s nguyn khng du. Nu thnh cng hm tr v m k t c ghi, nu tht bi hm tr v gi tr EOF.

51

Chng 1: Tp tin

int fgetc(FILE *fp); Hm c mt k t t tp tin fp. Nu thnh cng hm tr v m c c (c gi tr t 0 n 255), nu tht bi hoc gp cui tp tin hm tr v gi tr EOF. V d: Nhp vo tn mt tp tin vn bn. Hy c tng k t ca tp tin, nu k t c c l ch thng th i thnh ch hoa v ghi k t tr li tp tin.

52

Chng 1: Tp tin

void main() { char tentt[MAX_LEN]; <Nhap ten tap tin muon sua> int kq = DoiHoaTT(tentt); if(kq == 1) { printf(** Tap tin sau khi doi hoa **\n); DocTT(tentt); } else printf(Tap tin %s khong co tren dia\n, tentt); }

53

Chng 1: Tp tin

int DoiHoaTT(char *tentt) { FILE *fp; char ch; int vitri, ret; fp = fopen(tentt, r+b); if(fp != NULL) { while(!feof(fp)) { if((ch = fgetc(fp)) != EOF) { if(ch >= a && ch <= z) { ch -= 32; fseek(fp, -1, SEEK_CUR); fputc(ch, fp); fseek(fp, 1, SEEK_CUR); }} } fclose(fp); ret = 1; } else ret = 0;return ret; }
54

Chng 1: Tp tin

55