Input and OutputAnnouncementsSlide 3File TypesPlain Text filesData Files (pp. 98-102)Opening a Data File for InputReading from Data FileData file formatClose the file when finishedReading Until End of FileAverage Calculation ProgramWorking folderFriday’s ProgramInput Boxes (p.102)Input BoxesFormatting OutputPrint ZonesZones (p.104)Tabs (p.105)TabsWriting to a Data FilePrint to a Data FileInput and OutputInput and OutputAnnouncementsAnnouncementsExam Next WednesdayExam Next Wednesday–Next Monday: Review session.Next Monday: Review session.Invited talk:Invited talk:–7:30 PM ,Tuesday, Oct 28th.7:30 PM ,Tuesday, Oct 28th.–Prof. Katherine SochaProf. Katherine Socha –Mathematics: Morals and Mortals Mathematics: Morals and Mortals –5 extra credits for write-ups 5 extra credits for write-upsInput and OutputInput and OutputFile TypesFile TypesInput filesInput filesInput BoxesInput BoxesFormatting OutputFormatting OutputOutput filesOutput filesRead pp. 57-64Read pp. 57-64File TypesFile TypesSeveral file types are saved in “plain text” or Several file types are saved in “plain text” or ASCII formatted textASCII formatted text–.html, .txt, .csv, ….html, .txt, .csv, …These files can be read by almost all These files can be read by almost all programs, and can be used as inputprograms, and can be used as inputOther file types are saved in special Other file types are saved in special formatting and cannot be read easily formatting and cannot be read easily (.doc, .xls, .ppt, .exe, …)(.doc, .xls, .ppt, .exe, …)Plain Text filesPlain Text filesAll files are formed by bytes.All files are formed by bytes.Text files only use part of all possible bytes: Text files only use part of all possible bytes: The Basic ASCII code The Basic ASCII code –32 (20H) ~ 128 (80H) 32 (20H) ~ 128 (80H) –Every line end with byte 0DH and 0AH, also Every line end with byte 0DH and 0AH, also called CR (carriage return) and LF (line feed) called CR (carriage return) and LF (line feed) Any file that can be read by Any file that can be read by NotePad NotePad normallynormally is plain text and will work for data is plain text and will work for data filesfilesData Files (pp. 98-102)Data Files (pp. 98-102)A plain text data file (example data.txt) A plain text data file (example data.txt) can be read by a VB program. can be read by a VB program. 4444121233336464Create data file using Create data file using NotePad NotePad –Save it in Save it in same directorysame directory as program as programOpening a Data File for InputOpening a Data File for InputYou need to open a file for inputYou need to open a file for inputOpen “DATA.TXT” For Input As #1Open “DATA.TXT” For Input As #1Can use value 1-255 for “As #1”Can use value 1-255 for “As #1”““DATA.TXT” is the data file name.DATA.TXT” is the data file name.How does the program know where the file How does the program know where the file is?is?Reading from Data FileReading from Data FileCalled InputtingCalled InputtingInput #1, age1Input #1, age1Input #1, age2Input #1, age2OrOrInput #1, age1, age2Input #1, age1, age2General format:General format:Input #n, varNameInput #n, varNameData file formatData file formatData can be sepData can be sepaarated by line break rated by line break (CR/LF)(CR/LF)OOr can be separated by commar can be separated by commaIIf it is pure number file: separated by space.f it is pure number file: separated by space.Close the file when finishedClose the file when finishedClose #1 Close #1 ~~ ~~ the file reference numberthe file reference numberUsing a Do while loop, you can read until you reach Using a Do while loop, you can read until you reach the end of the filethe end of the fileDo While Not EOF(1)Do While Not EOF(1)Reading Until End of FileReading Until End of FileThe The EOF(file reference number)EOF(file reference number) function function will return TRUE, when the file pointer will return TRUE, when the file pointer reaches the end of file.reaches the end of file.Using a Do while loop, you can read until Using a Do while loop, you can read until you reach the end of the fileyou reach the end of the fileDo While Not EOF(1)Do While Not EOF(1)‘‘Processing code hereProcessing code hereInput #1, variableInput #1, variableLoopLoopAverage Calculation ProgramAverage Calculation ProgramPrivate Sub AvgCalc_Click()Private Sub AvgCalc_Click()Dim num as single, Sum as single, count as integerDim num as single, Sum as single, count as integerDim average as singleDim average as singleOpen “data.txt” for input as #1Open “data.txt” for input as #1Sum = 0Sum = 0Count = 0Count = 0Do While Not EOF (1)Do While Not EOF (1)Input #1, numInput #1, numSum = Sum + numSum = Sum + numCount = Count + 1Count = Count + 1LoopLoopClose #1Close #1Average = Sum/CountAverage = Sum/CountPicture1.print AveragePicture1.print AverageEnd SubEnd SubWorking folderWorking folderThe visual basic’s default working folder is in the The visual basic’s default working folder is in the path, where the vb6.exe file resides.path, where the vb6.exe file resides.Open a form file directly from its folder will set Open a form file directly from its folder will set ththe e working folder to the form file folder.working folder to the form file folder.App.Path will give the name of the folder where App.Path will give the name of the folder where the .frm file is. the .frm file is. –YYou must save the project first, otherwise the working ou must save the project first, otherwise the working folder is still same as vb6.exefolder is still same as vb6.exe–Open App.Path & “\data.txt” For Input As Open App.Path & “\data.txt” For Input As #1#1Friday’s ProgramFriday’s ProgramCreate a program that calculates the Create a program that calculates the Standard Deviation of a datasetStandard Deviation of a datasetStandard deviation is the average distance Standard deviation is the average distance between a point and the meanbetween a point and the meanSd = sqr(Sd = sqr(∑(num-average)/(n-1))∑(num-average)/(n-1))Input Boxes (p.102)Input Boxes (p.102)Dim temptext As StringDim temptext As Stringtemptext = InputBox("Give me a value", temptext = InputBox("Give me a value", __ "Enter Value")"Enter Value")miles = Val(temptext)miles = Val(temptext)__ (underscore) (underscore) is line continuation characteris line continuation
View Full Document