230 likes | 462 Views
Input and Output. Announcements. Exam Next Wednesday Next Monday: Review session. Invited talk: 7:30 PM ,Tuesday, Oct 28th. Prof. Katherine Socha Mathematics: Morals and Mortals 5 extra credits for write-ups. Input and Output. File Types Input files Input Boxes Formatting Output
E N D
Announcements • Exam Next Wednesday • Next Monday: Review session. • Invited talk: • 7:30 PM ,Tuesday, Oct 28th. • Prof. Katherine Socha • Mathematics: Morals and Mortals • 5 extra credits for write-ups
Input and Output • File Types • Input files • Input Boxes • Formatting Output • Output files • Read pp. 57-64
File Types • Several file types are saved in “plain text” or ASCII formatted text • .html, .txt, .csv, … • These files can be read by almost all programs, and can be used as input • Other file types are saved in special formatting and cannot be read easily (.doc, .xls, .ppt, .exe, …)
Plain Text files • All files are formed by bytes. • Text files only use part of all possible bytes: The Basic ASCII code • 32 (20H) ~ 128 (80H) • Every line end with byte 0DH and 0AH, also called CR (carriage return) and LF (line feed) • Any file that can be read by NotePadnormally is plain text and will work for data files
Data Files (pp. 98-102) • A plain text data file (example data.txt) can be read by a VB program. 44 12 33 64 • Create data file using NotePad • Save it in same directory as program
Opening a Data File for Input • You need to open a file for input Open “DATA.TXT” For Input As #1 • Can use value 1-255 for “As #1” • “DATA.TXT” is the data file name. • How does the program know where the file is?
Reading from Data File • Called Inputting Input #1, age1 Input #1, age2 Or Input #1, age1, age2 • General format: Input #n, varName
Data file format • Data can be separated by line break (CR/LF) • Or can be separated by comma • If it is pure number file: separated by space.
Close the file when finished Close #1 ~~ the file reference number Using a Do while loop, you can read until you reach the end of the file Do While Not EOF(1)
Reading Until End of File • The EOF(file reference number) function will return TRUE, when the file pointer reaches the end of file. • Using a Do while loop, you can read until you reach the end of the file Do While Not EOF(1) ‘Processing code here Input #1, variable Loop
Average Calculation Program Private Sub AvgCalc_Click() Dim num as single, Sum as single, count as integer Dim average as single Open “data.txt” for input as #1 Sum = 0 Count = 0 Do While Not EOF (1) Input #1, num Sum = Sum + num Count = Count + 1 Loop Close #1 Average = Sum/Count Picture1.print Average End Sub
Working folder • The visual basic’s default working folder is in the path, where the vb6.exe file resides. • Open a form file directly from its folder will set the working folder to the form file folder. • App.Path will give the name of the folder where the .frm file is. • You must save the project first, otherwise the working folder is still same as vb6.exe • Open App.Path & “\data.txt” For Input As #1
Friday’s Program • Create a program that calculates the Standard Deviation of a dataset • Standard deviation is the average distance between a point and the mean • Sd = sqr(∑(num-average)/(n-1))
Input Boxes (p.102) Dim temptext As String temptext = InputBox("Give me a value", _ "Enter Value") miles = Val(temptext) _ (underscore) is line continuation character
Input Boxes • Inputs a string variable • If you want to use as a number, you must input string and then convert miles = Val(temptext)
Formatting Output • Putting Multiple output on one line Picture1.Print kilometers; " Kilometers" • Tabs and Zones • Can specify width with tabs, zones are fixed
Print Zones 1 15 29 43 Nan Zhang Saturday
Zones (p.104) • Just separate output pieces with comma • Each zone 14 characters wide PicBox.Print “Nan”, “Zhang”, _ “Saturday” NanZhang Saturday
Tabs (p.105) • Tab(n); - put a number in for n • Must use semicolons between pieces Pic.Print “Nan”; Tab(10); “Zhang”; _ Tab(20); “Saturday”; Tab(30); “1000” • Notice that tab number changes - it is the location of the column, not the amount of space
Tabs 1 10 20 30 Nan Zhang Saturday 1000
Writing to a Data File Open “data.txt” for Output as #9 Write #9, age1 Write #9, age2 Write #9, age3 … Close #9 • Will write delimited data to a file • “Hello”, “world”, 114, 132, “What ever” • You can also use print command. It will write display-formatted data to the file.
Print to a Data File Open “data.txt” for Output as #9 Print #9, age1, age2, age3 Print #9, age4 Print #9, age5 … Close #9 • You can also use print command. It will write display-formatted data to the file. • Just like the form you print in the picture box.