Académique Documents
Professionnel Documents
Culture Documents
IntroductiontoRAPTOR:DataFiles CreatingandDisplayingDataFiles
InRAPTORwecancreatedatafilesandreadfromthefiles.However,sorting,insertingrecords,or mergingtwodatafilesrequiressomefancyfootworkbutifyoureveryambitious,tryit!
TheRedirect_Output Procedure
Tocreateadatafile,youusea Call to the Redirect_Output procedure.RAPTORprovides twoversionsofthisprocedure. 1. Afilenameisusedasanargumentto Redirect_Output, asshowninthefollowing examples: Redirect_Output("sample.txt") Redirect_Output("C:\MyDocuments\John.Doe\sample")
Notethatinthefirstexample,onlythefilenameisgiven.Inthiscase,thespecifiedtextfile willbecreatedinthesamedirectoryasthecurrentRAPTORprogram.Inthesecond example,thefullpathtothefileisgiven.Also,inthesecondexample,nofileextensionis specified.Inthiscase,thefile sample willbecreatedwithnoextension. 2. Youcaneitherturnonoroff Redirect_Output byincludingasimpleyes/true or no/false argument,asfollows: Redirect_Output(True)or Redirect_Output(yes) Redirect_Output(False) or Redirect_Output(no)
Now,theoutputmustberedirectedtothedatafilebyusinga Calltothe Redirect_Output procedure.Thenameofthedatafileisusedastheargument.Thisfilenamemustbeinside quotationmarks(asshownintheexamplesabove). Next,createthecodetoinputdata.Onevariableisrequiredforeachfieldoftherecordsinthe datafile.The Output boxwill PUT thevalueofthosevariablesintoeachrecord.Forexample,to
1 of 5
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011 createadatafilewithrecordsthathavetwofields, Name and Salary,twovariablesarerequired (probablycalled Name and Salary).Asvaluesfordifferentemployeesareinput,each Name and Salary willbestoredinthedatafileonasingleline. Afterthedatahasbeenentered,the Redirect_Output mustbeturnedoff.A Callisusedto callthe Redirect_Output procedureagain,butthistimeitsturnedoffbyusingeither no or false astheargument. Figure1(followingpage)showsaRAPTORflowchartthatwillwritetworecords,eachwithtwo fields,toadatafilenamed sample.txt. Figure2showsthecontentsofthefilecreated(and openedinNotepad).
2 of 5
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure1Programtowriterecordsto adatafile
Figure2Textfilecreated
3 of 5
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
TheEnd_Of_InputFunction
RAPTORsbuiltinfunction, End_Of_Input,canbeusedasthetestconditionofaloop.When readingrecordsinadatafile,ifthisfunctionisusedasthetestcondition,RAPTORwillendthe loopwhenalltherecordshavebeenread. Whenalltherecordshavebeenreadandwrittentothe Master Console, the Redirect_Input proceduremustbeturnedoffwitha Call totheprocedureusing False or no fortheargument.
HowtheContentsoftheFileareStored
The Redirect_Inputproceduredoesnotseparateeachfieldinarecord.Rather,eachrecord isstoredasonelineofstringdata.Each Inputlinereadsallthefieldsinonerecord(or everythingthatisonasinglelineinthedatafile).Therefore,therecordscanbeoutputtothe Master Console butthefieldscannotbemanipulatedeasilytosort,insert,ormerge.Thiscan bedone,butitrequiresadvancedprogramming. Figure3showsasampleofthecodetoreadrecordsfromadatafile(sample.txt)andthe Master Console display.
4 of 5
edited from Appendix D: Introduction to RAPTOR Prelude to Programming: Concepts and Design, 5th edition by Elizabeth Drake and Stewart Venit Addison-Wesley Pub. 2011
Figure3Readingrecordsfromadatafileanddisplayingthem
5 of 5