You are on page 1of 6

Tun:

Tit PPCT:
Ngi son: Nguyn Vn Nguyn
Bi 15. Thao tc vi tp
I.

Mc tiu
1. Kin thc
- Bit cc bc lm vic vi tp: gn tn cho bin tp, m tp, c/ghi tp, ng tp.
- Bit khai bo bin tp v cc thao tc c bn vi tp vn bn.
- Bit s dng mt s hm v th tc chun lm vic vi tp.
2. K nng
- Khai bo ng bin kiu tp.
- Thc hin c thao tc x l tp: Gn tn tp, m/ng tp, c/ghi tp.
- S dng c cc th tc lin quan c/ghi d liu ca tp.
3. Thi
- Gp phn rn luyn t duy v tc phong lp trnh.
- Rn luyn cho hc sinh c thc lu tr d liu mt cch khoa hc, phng chng mt
mt thng tin hoc nhim virt .

II.

Phng php, phng tin dy hc


- Phng php: Din ging, t vn , gi m, vn p.
- Phng tin: Gio n, SGK.

III.

Tin trnh by dy
1. n nh lp:

2. Kim tra bi c:
3. Ni dung bi mi:
Tg

Hot ng gio vin


-C hai thao tc c bn i

Hot ng hc sinh

Ni dung

vi tp l ghi d liu vo tp
v c d liu t tp ra . Ta
xt xem trong Pascal cc
thao tc c th hin
nh th no i vi tp vn
bn ?
Hot ng 1: Khai bo kiu

Bi 15 : Thao tc vi tp

tp.

1. Khai bo

-Vit khai bo bin tp ln -Ch nghe ging, ly thm Var <tn bin tp> : Text ;
bng ri gii thch cc t v d v khai bo tp.

V d: Var t1, t2 : text;

kha, tn bin tp hc
sinh hiu c .
Hot ng 2: Thao tc vi

2. Thao tc vi tp

tp
Gii thiu cc thao tc vi
tp.
Cc thao tc vi tp chia
thnh bn nhm :
- Gn tn tp.
- M tp.
- c/ghi tp vn bn.
- ng tp.
Hot ng 2.1: Gn tn tp

-Ch nghe ging v ghi

a) Gn tn tp

lp 10 ta hc tn tp, chp bi
mi tp u c t mt tn
vd: tinhoc.txt
Mi tp d liu u c mt
tn tp tham chiu. y
ta khng thao tc trc tip vi
tp d liu trn a m thng
qua tn bin tp. Bi vy ta
phi to mt tham chiu gia
tn tp v bin tp.
Do vy, thao tc vi
tp,trc ht ta phi gn tn
tp vi i din ca n l bin
tp bng th tc:
assign(<bin tp>,<tn tp>);

- Theo di v d v ghi chp assign(<bin

bi.

tp>,

<tn

tp>);

Trong : Tn tp l bin xu

Trong : Tn tp l bin

hoc hng xu.

xu hoc hng xu.

V d 1: Gi thit c bin xu

V d 1:

MYFILE v cn gn bin

MYFILE:= 'DULIEU.DAT';

tp F2 vi tp c tn

assign(F2,MYFILE);

DULIEU.DAT. Vic gn tn

hoc

tp c thc hin bng cc

assign(F2,'DULIEU.DAT');

cu lnh:
MYFILE:= 'DULIEU.DAT';
assign(F2,MYFILE);
hoc
assign(F2,'DULIEU.DAT');
Hi: v d 1: em hy ch ra -Hng xu l DULIEU.DAT
ch no l hng xu, ch -Bin xu l MYFILE
no l bin xu?
V d 2: c th c d
liu t tp KETQUA.DAT

assign(F3,C:\KETQUA.DA V d 2:
T);

trn th mc gc a C: Ta

c tp

KETQUA.DAT trn a
C:

dng cc cu lnh sau

assign(F3,C:\KETQUA.DA

gn n vi tp F3?

T);

- Cho v d gi hc sinh tr
li

b) M tp

Hot ng 2.2: M tp
- Gi s quyn tp tin hc ca -Tr li theo nhn nh.

chng ta l tp. By gi
chng ta m tp (l m
quyn tp) ra lm g?
C 2 trng hp m tp
ra, l:
-M tp ra ghi bi

-Ghi chp vo v.

-M tp ra c (khi v nh
th ta m ra hc bi).
i vi tp trong Pascal th
cng c 2 cch m.
- M tp ra ghi d liu

-Ghi nh.

- Th tc m tp ghi d

liu vo a:

-Vi th tc ny ta c th - Cha, trc khi m tp phi

rewrite(<bin tp>);

m tp ghi d liu c gn tn tp vi bin tp bng V d:


cha?

th tc assign

assign(t3,C:\TEP.DAT);
rewrite(t3);

- M tp ra c d liu

- Tng t cng phi c th -Th tc m tp c d


tc assign trc khi m tp

liu:

- Cho v d gi hc sinh tr

reset(<bin tp>);

li
- Khi

V d:
thc

hin

th

tc

tentep:= DL.DAT;

rewrite(t3), nu trn th mc

assign(tep1,tentep);

gc ca a C cha c tp

reset(tep1);

TEP.DAT, th tp s c
to vi ni dung rng. Nu
c, th ni dung c s b
xa chun b ghi d liu
mi.
GV nhn mnh: trc khi s
dng hai th tc trn phi
gn tn tp cho bin tp
ng thi bin tp phi c
khai bo t trc.

-Ln bng trnh by.

Hot ng 2.3: c/ghi tp

c)

vn bn

bn.

-i vi cc kiu d liu -Ch v ghi chp.


khc ta c nhp/xut nhng
i vi kiu d liu tp th
c/ghi.
-Th tc read ging nh nhp
d liu t bn phm m ta
hc
-Yu cu hs cho v d.

c)c/ghi

tp

vn

c d liu t tp:

read(<bin tp>,<danh sch


bin>);
hoc
readln(<bin tp>,<danh sch
bin>);
V d:
readln(f,x1,x2): c d liu
t bin tp f, t gi tr vo
hai bin x1, x2.

- Th tc write cng ging -Ch v ghi chp.


nh cu lnh xut d liu ra
mn hnh m ta hc
phn trc.

Ghi d liu vo tp:

write(<bin tp>,<danh sch


kt qu>);
hoc
writeln(<bin

tp>,<danh

sch kt qu>);
VD: writeln(g,Tong la , x1
+ x2): ghi vo bin tp g hai
- Cho v d gi hc sinh tr

tham s: dng ch Tong la

li

v gi tr tng x1+ x2.

-Gii thiu hc sinh bit


hai hm chun thng dng
v ngha ca n.

d) ng tp
Th tc ng tp c dng:
close(<bin tp>);
V d: close(F1);
close(F3);

Hot ng 2.3: ng tp
-Sau khi lm vic xong vi
tp cn phi ng tp. Vic
ng tp l c bit quan
trng sau khi ghi d liu, khi
h thng mi tht s
hon tt vic ghi d liu ra
tp.
-Sau khi ng, mt tp vn c
th c m li. Khi m li
tp, nu vn dng bin tp
c th khng cn phi dng
th tc assign gn li tn
tp.

4. Cng C
- c im kiu d liu tp.
- S khc bit gia tp vn bn v tp cu trc.
- Khai bo kiu tp.
- Cc thao tc lm vic vi tp ( Minh ha bng hnh v sau:)
ASSIGN(<bin tp>,<tn tp);

Ghi

REWRITE(<bin tp>);

RESET(<bin tp>);

WRITE(<bin tp>,<ds kt qu>);

READ(<bin tp >,<danh sch bin>);

CLOSE(<bin tp>);

5. Dn d

S lm vic vi tp

- Cc em v xem li cc kin thc v kiu d liu tp v xem trc cc v d v tp bi 16.


IV. Rt kinh nghim
.............................................................................................................................................
.............................................................................................................................................
.............................................................................................................................................
.............................................................................................................................................
.............................................................................................................................................
.............................................................................................................................................
.............................................................................................................................................