1 / 23

Input and Output

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

brad
Download Presentation

Input and Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Input and Output

  2. 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

  3. Input and Output • File Types • Input files • Input Boxes • Formatting Output • Output files • Read pp. 57-64

  4. 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, …)

  5. 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

  6. 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

  7. 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?

  8. Reading from Data File • Called Inputting Input #1, age1 Input #1, age2 Or Input #1, age1, age2 • General format: Input #n, varName

  9. 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.

  10. 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)

  11. 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

  12. 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

  13. 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

  14. 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))

  15. Input Boxes (p.102) Dim temptext As String temptext = InputBox("Give me a value", _ "Enter Value") miles = Val(temptext) _ (underscore) is line continuation character

  16. Input Boxes • Inputs a string variable • If you want to use as a number, you must input string and then convert miles = Val(temptext)

  17. Formatting Output • Putting Multiple output on one line Picture1.Print kilometers; " Kilometers" • Tabs and Zones • Can specify width with tabs, zones are fixed

  18. Print Zones 1 15 29 43 Nan Zhang Saturday

  19. Zones (p.104) • Just separate output pieces with comma • Each zone 14 characters wide PicBox.Print “Nan”, “Zhang”, _ “Saturday” NanZhang Saturday

  20. 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

  21. Tabs 1 10 20 30 Nan Zhang Saturday 1000

  22. 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.

  23. 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.

More Related