input and output. announcements exam next wednesday –next monday: review session. invited talk:...
TRANSCRIPT
AnnouncementsAnnouncements
Exam 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-ups
Input and OutputInput and Output
File TypesFile Types Input filesInput files Input BoxesInput Boxes Formatting OutputFormatting Output Output filesOutput files Read pp. 57-64Read pp. 57-64
File TypesFile Types
Several 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 input
Other 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 files
All 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 filesfiles
Data 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.
4444121233336464 Create data file using Create data file using NotePad NotePad
– Save it in Save it in same directorysame directory as program as program
Opening a Data File for InputOpening a Data File for Input
You need to open a file for inputYou need to open a file for input
Open “DATA.TXT” For Input As #1Open “DATA.TXT” For Input As #1
Can 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 File
Called InputtingCalled Inputting
Input #1, age1Input #1, age1
Input #1, age2Input #1, age2
OrOr
Input #1, age1, age2Input #1, age1, age2 General format:General format:
Input #n, varNameInput #n, varName
Data file formatData file format
Data 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 comma IIf 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 finished
Close #1 Close #1 ~~ ~~ the file reference numberthe file reference number
Using 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 file
Do While Not EOF(1)Do While Not EOF(1)
Reading Until End of FileReading Until End of File
The 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 here
Input #1, variableInput #1, variable
LoopLoop
Average Calculation ProgramAverage Calculation Program
Private 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 + 1
LoopLoopClose #1Close #1Average = Sum/CountAverage = Sum/CountPicture1.print AveragePicture1.print AverageEnd SubEnd Sub
Working folderWorking folder
The 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#1
Friday’s ProgramFriday’s Program
Create a program that calculates the Create a program that calculates the Standard Deviation of a datasetStandard Deviation of a dataset
Standard deviation is the average distance Standard deviation is the average distance between a point and the meanbetween a point and the mean
Sd = 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 character
Input BoxesInput Boxes
Inputs a string variableInputs a string variable If you want to use as a number, you must If you want to use as a number, you must
input string and then convertinput string and then convertmiles = Val(temptext)miles = Val(temptext)
Formatting OutputFormatting Output
Putting Multiple output on one linePutting Multiple output on one linePicture1.Print kilometers; " Kilometers"Picture1.Print kilometers; " Kilometers"
Tabs and ZonesTabs and Zones– Can specify width with tabs, zones are fixedCan specify width with tabs, zones are fixed
Zones (p.104)Zones (p.104)
Just separate output pieces with commaJust separate output pieces with comma Each zone 14 characters wideEach zone 14 characters widePicBox.Print “Nan”, “Zhang”, PicBox.Print “Nan”, “Zhang”, __
““Saturday”Saturday”
Nan Zhang SaturdayNan Zhang Saturday
Tabs (p.105)Tabs (p.105)
Tab(n); - put a number in for nTab(n); - put a number in for n Must use semicolons between piecesMust use semicolons between piecesPic.Print “Nan”; Tab(10); “Zhang”; _ Pic.Print “Nan”; Tab(10); “Zhang”; _ Tab(20); “Saturday”; Tab(30); “1000” Tab(20); “Saturday”; Tab(30); “1000”
Notice that tab number changes - it is Notice that tab number changes - it is the location of the column, not the the location of the column, not the amount of spaceamount of space
Writing to a Data FileWriting to a Data File
Open “data.txt” for Output as #9Open “data.txt” for Output as #9
Write #9, age1Write #9, age1
Write #9, age2Write #9, age2
Write #9, age3Write #9, age3
… …
Close #9Close #9
WWiill write delimited data to a filell write delimited data to a file– ““Hello”, “world”, 114, 132, “What ever”Hello”, “world”, 114, 132, “What ever”
YYoou can also use print command. It will write display-u can also use print command. It will write display-formatted data to the file.formatted data to the file.
Print to a Data FilePrint to a Data File
Open “data.txt” for Output as #9Open “data.txt” for Output as #9
Print #9, age1, age2, age3Print #9, age1, age2, age3
Print #9, age4Print #9, age4
Print #9, age5Print #9, age5
… …
Close #9Close #9
YYoou can also use print command. It will write display-u can also use print command. It will write display-formatted data to the file. formatted data to the file.
Just like the form you print in the picture box.Just like the form you print in the picture box.