Chapt 17: Fls Stong Infomaton Fo Lat. Pag 255 Chapt 17: Fls Stong Infomaton Fo Lat. W hav xplod th comput's shot tm mmoy wth vaabls and aays but how do w sto thos valus fo lat? Th a many dffnt tchnqus fo long tm data stoag. BASIC-256 suppots wtng and adng nfomaton fom fls on you had dsk. That pocss of nput and output s oftn wttn as I/O. Ths chapt wll show you how to ad valus fom a fl and thn wt thm fo long tm stoag. Radng Lns Fom a Fl: Ou fst pogam usng fls s gong to show you many of th statmnts and constants you wll nd to us to manpulat fl data. Th a sval nw statmnts and functons n ths pogam. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # c17_adlfl.kbs # ad a smpl txt fl nput "fl nam> ", fn$ f not xsts(fn$) thn pnt fn$ + " dos not xst." nd nd f # n = 0 opn fn$ whl not of l$ = adln n = n + 1 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. 15 16 17 18 19 Pag 256 pnt n + " " + l$ nd whl # pnt "th fl " + fn$ + " s " + sz + " byts long." clos Pogam 110: Rad Lns Fom a Fl fl nam> 2_c17_tst.txt 1 Ths a th tms that 2 ty mn's souls. 3 - Thomas Pan th fl 2_c17_tst.txt s 57 byts long. Sampl Output 110: Rad Lns Fom a Fl xst(xpsson) Look on th comput fo a fl nam spcfd by th stng xpsson. Dv and path may b spcfd as pat of th fl nam, but f thy a omttd thn th cunt wokng dctoy wll b th sach locaton. Rtuns tu f th fl xsts; ls tuns fals. 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. opn opn opn opn Pag 257 xpsson (xpsson) flnumb, xpsson (flnumb, xpsson) Opn th fl spcfd by th xpsson fo adng and wtng to th spcfd fl numb. If th fl dos not xst t wll b catd so that nfomaton may b addd (s wt and wtln). B su to xcut th clos statmnt whn th pogam s fnshd wth th fl. BASIC-256 may hav a total of ght (8) fls opn 0 to 7. If no fl numb s spcfd thn th fl wll b opnd as fl numb zo (0). of of() of(flnumb) Th of functon tuns a valu of tu f w a at th nd of th fl fo adng o fals f th s stll mo data to b ad. If flnumb s not spcfd thn fl numb zo (0) wll b usd. 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 258 adln adln() adln(flnumb) Rtun a stng contanng th contnts of an opn fl up to th nd of th cunt ln. If w a at th nd of th fl [ of(flnumb) = tu ] thn ths functon wll tun th mpty stng (""). If flnumb s not spcfd thn fl numb zo (0) wll b usd. sz sz() sz(flnumb) Ths functon tuns th lngth of an opn fl n byts. If flnumb s not spcfd thn fl numb zo (0) wll b usd. clos clos() clos flnumb clos(flnumb) Th clos statmnt wll complt any pndng I/O to th fl and allow fo anoth fl to b opnd wth th sam numb. If flnumb s not spcfd thn fl numb zo (0) wll b usd. 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 259 Wtng Lns to a Fl: In Pogam 110 w saw how to ad lns fom a fl. Th nxt two pogams show dffnt vaatons of how to wt nfomaton to a fl. In Pogam 111 w opn and cla any data that may hav bn n th fl to add ou nw lns and n Pogam 112 w appnd ou nw lns to th nd (savng th pvous data). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # c17_stwt.kbs # wt txt to a fl, go back to bgnng # and dsplay th txt opn "2_c17_stwt.dat" pnt "nt a blank ln to clos fl" # cla fl (st) and stat ov st whl tu nput ">", l$ f l$ = "" thn xt whl wtln l$ nd whl # go th th stat and dsplay contnts sk 0 k = 0 whl not of() k = k + 1 pnt k + " " + adln() nd whl clos nd Pogam 111: Cla Fl and Wt Lns 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 260 nt a blank ln to clos fl >ths s som >data, I am typng >nto th pogam. > 1 ths s som 2 data, I am typng 3 nto th pogam. Sampl Output 111: Cla Fl and Wt Lns st o st() o st flnumb st(flnumb) Cla any data n an opn fl and mov th fl pont to th bgnnng. If flnumb s not spcfd thn fl numb zo (0) wll b usd. 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 261 sk xpsson sk(xpsson) sk flnumb,xpsson sk (flnumb,xpsson) Mov th fl pont fo th nxt ad o wt opaton to a spcfc locaton n th fl. To mov th cunt pont to th bgnnng of th fl us th valu zo (0). To sk to th nd of a fl us th sz() functon as th agumnt to th s statmnt. If flnumb s not spcfd thn fl numb zo (0) wll b usd. wtln xpsson wtln(xpsson) wtln flnumb,xpsson wtln (flnumb,xpsson) Output th contnts of th xpsson to an opn fl and thn appnd an nd of ln mak to th data. Th fl pont wll b postond at th nd of th wt so that th nxt wt statmnt wll dctly follow. If flnumb s not spcfd thn fl numb zo (0) wll b usd. 1 2 3 4 5 6 7 # c17_appndwt.kbs # appnd nw lns on th nd of a fl # thn dsplay t opn "2_c17_appndwt.dat" pnt "nt a blank ln to clos fl" 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Pag 262 # mov fl pont to nd of fl and appnd sk sz whl tu nput ">", l$ f l$ = "" thn xt whl wtln l$ nd whl # mov fl pont to bgnnng and show contnts sk 0 k = 0 whl not of() k = k + 1 pnt k + " " + adln() nd whl clos nd Pogam 112: Appnd Lns to a Fl nt a blank ln to clos fl >sd sd sd >vm vm vm > 1 ba ba ba 2 foo foo foo 3 gap gap gap 4 sd sd sd 5 vm vm vm Sampl Output 112: Appnd Lns to a Fl 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 263 Rad() Functon and Wt Statmnt: In th fst th pogams of ths chapt w hav dscussd th adln() functon and wtln statmnt. Th a two oth statmnts that wll ad and wt a fl. Thy a th ad() functon and wt statmnt. ad ad() ad(flnumb) Rad th nxt wod o numb (tokn) fom a fl. Tokns a dlmtd by spacs, tab chaacts, o nd of lns. Multpl dlmts btwn tokns wll b tatd as on. If flnumb s not spcfd thn fl numb zo (0) wll b usd. wt wt wt wt xpsson (xpsson) flnumb,xpsson (flnumb,xpsson) Wt th stng xpsson to a fl fl. Do not add an nd of ln o a dlmt. If flnumb s not spcfd thn fl numb zo (0) wll b usd. 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 264 Ths pogam uss a sngl txt fl to hlp us mantan a lst of ou fnd's tlphon numbs. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # c17_phonlst.kbs # add a phon numb to th lst and show flnam$ = "c17_phonlst.txt" pnt "phonlst.kbs - Manag you do nput "Add, Lst, Qut (a/l/q)? f lft(low(acton$),1) = "a" addcod(flnam$) f lft(low(acton$),1) = "l" lstfl(flnam$) untl lft(low(acton$),1) = "q" nd phon lst." ",acton$ thn call thn call suboutn lstfl(f$) f xsts(f$) thn # lst th nams and phon numbs n th fl opn f$ pnt "th fl s " + sz + " byts long" whl not of # ad nxt ln fom fl and pnt t pnt adln nd whl clos ls pnt "No phons on fl. Add fst." nd f 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. 27 28 29 30 31 32 33 34 35 36 37 38 Pag 265 nd suboutn suboutn addcod(f$) nput "Nam to add? ", nam$ nput "Phon to add? ", phon$ opn f$ # sk to th nd of th fl sk sz() # w a at nd of fl - add nw ln wtln nam$ + ", " + phon$ clos nd suboutn Pogam 113: Bg Pogam - Phon Lst phonlst.kbs - Manag you phon lst. Add, Lst, Qut (a/l/q)? l th fl s 46 byts long jm, 555-5555 sam, 555-7777 doug, 555-3333 Add, Lst, Qut (a/l/q)? a Nam to add? ang Phon to add? 555-0987 Add, Lst, Qut (a/l/q)? l th fl s 61 byts long jm, 555-5555 sam, 555-7777 doug, 555-3333 ang, 555-0987 Add, Lst, Qut (a/l/q)? q Sampl Output 113: Bg Pogam - Phon Lst 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 266 Excss: s n p o t b n y f p x k k m s l o o s z t n w t t t l z t c s s m l k q f d s l t h l a o y j w s l a d d p c o f d n clos, dlmt, dctoy, of, xsts, fl, opn, ad, adln, st, sk, sz, tokn, wt, wtln wods 17.1. Cat a fl n th dctoy wh you sav you pogams namd numbs.txt. Opn t wth a txt dto, lk Notpad n Wndows o gedt n LINUX, and typ n a lst of dcmal numbs. Put ach on on a spaat ln. Now wt a pogam to ad th numbs fom th fl, on ln at a tm. Calculat th total of th numbs n th fl and th avag. Rmmb to us th float() functon to convt th stng you ad fom th fl to a numc valu bfo you add t to th unnng total. 17.2. Cat a fl n th dctoy wh you sav you pogams namd popl.txt. Opn t wth a txt dto, lk Notpad n 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 267 Wndows o gedt n LINUX, and typ n th data blow. Jm,M,47 May,F,23 Bob,M,67 John,M,13 Su,F,18 Wt a pogam that wll ad n th data fom th popl fl. Us stng handlng functons fom Chapt 16 to bak ach ln nto th pats: 1) nam, 2) gnd, and 3) ag. Tally th total of th ags, th numb of popl, and th numb of mals as you ad th fl. Onc you hav ad all of th cods dsplay th pcntag of mals and th avag ag of th popl n th fl. 17.3. Cat a fl n th dctoy wh you sav you pogams namd assgnmnts.txt. Opn t wth a txt dto, lk Notpad n Wndows o gedt n LINUX, and typ n th data blow. Jm,88,45 Jo,90,33 May,54,29 Mauy, 57,30 Wt a pogam that wll ad n th data fom th assgnmnts fl and wt out a nw fl namd fnalgad.txt wth th studnt's nam, a comma, and th cous gad. Calculat th cous gad fo ach studnt basd on th two assgnmnt gads. Th fst assgnmnt was woth 100 ponts and th scond assgnmnt was woth 50 ponts. Th output should look somthng lk: Jm,88 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)
Chapt 17: Fls Stong Infomaton Fo Lat. Pag 268 2014 Jams M. Rnau (CC BY-NC-SA 3.0 US)