/---------------------\ | Notes for lecture04 | | 21 September 1999 | | CS 125-609 | | Michael Goldwasser | \---------------------/ ========================================================================= Programming Style: * Option Explicit * ' using comments * indenting based on loops or conditionals or subroutines * descriptive variable names * break long lines by placing the underscore ("_") character at end * break program into natural "chunks" (more on this next time) ========================================================================= Input/Output: "I have been dreading this topic" -- lots of details -- each language is quite different in the way in handles I/O (why? so many different input and output device types.) But we do need to do it. - Important to get information to/from user. - Must also learn to read/save files because you cannot have many useful programs which force user to start from scratch every time they startup the program -------------------------------------------------------------------------- * can read strings from user through textbox (text1.text) can get MUCH input from user by way of control properties -------------------------------------------------------------------------- * can write strings to user through label,textbox (label1.caption, text1.text) -------------------------------------------------------------------------- * can also use picbox's for displaying output. (No longer exactly strings, but figures, and pictorial representations of strings) Syntax: Numeric n: pic1.Print n Generates one full line of output with representation of n - all numbers printed with TRAILING space - for nonnegative numbers, prints a LEADING space Strings s: pic1.Print s NEWLINE: default is that print will end with a CR/NL To leave pic1.Print n; (notice the SEMICOLON) Can show multiple things in one print command (whether on many lines or one) pic1.Print n; x y Clearing the screen: pic1.Cls NO LONGER STRINGS ONCE DISPLAYED: it is displaying the pixels. The string is lost. Also, characters may be different widths depending on font. PRINT ZONES: the same as tab stops on a typewriter. By default, each zone contains an "average 14 characters". Syntax: pic1.Print "Here", "we", "go" displays as Here we go SPECIFIC TABS: pic1.Print Tab(10); "Hello" will try to display the word starting at the 10th position. -------------------------------------------------------------------------- * can even write directly to a printer. (Printer1.Print method) -------------------------------------------------------------------------- There are special widgets already included with the language for special purpose input or output events. (See InputBox and MsgBox) -------------------------------------------------------------------------- * Files -- useful for both input and output To Open a file for use, you must: -- give the filename -- assign it a reference number for your use -- state your intentions for the use of the file Examples: Open "myfile" For Input As #1 Open "D:/userdata/myfile" For Output As #25 Open "logfile" For Append As #x Reading from an open file: Dim X as Integer Input #5, x Dim name as String Input #5, name Can read many items in one statement Input #5, x, name FILE FORMAT: for this use, items in a file are officially separated by a comma, or by a line break. Also, strings are enclosed in quotes (however numbers are not) End of File: You may check whether or not you have reached the end of an input file #8 by using the expression, EOF(8). This is often useful, for example, in writing a loop that reads an entire file, e.g., Do While not EOF(8) Input #8, x pic1.Print x Loop Writing to an open file: For a single variable (number or string) Syntax: Write #n, x For multiple variables to be output on one line, separated by commas Syntax: Write #n, x, myname, y YOU MUST CLOSE A FILE (or all work is lost!): Syntax: Close #5 -------------------------------------------------------------------------- Manipulating files: Dir("filename") will return filename if that file exists already or will return "" if the file does NOT exist. Kill("filename") will erase the file (moves to trashcan???) Name "filename" As "newfilename" will rename a file NOTE: you cannot kill or rename currently opened files. =========================================================================