Programming with Visual Basic NET Input and Output

Programming with Visual Basic. NET Input and Output Files Lecture # 6 Tariq Ibn Aziz Compunet Corporation 1

Opening Files for Sequential Access • When you open a file for sequential access, you must specify the file mode, Input, Output, and Append. • To open a file for sequential access File. Open (File. Number, File. Name, Open. Mode. Input) Compunet Corporation 2

File. Open Function • You must open a file before any I/O operation can be performed on it File Mode File. Open(1, "TESTFILE", Open. Mode. Input) ' Close before reopening in another mode. File. Close(1) File Number File Name • Microsoft. Visual. Basic namespace must be imported Compunet Corporation 3

File. Open Function • This example opens the file in Binary mode for writing operations only. File. Open(1, "TESTFILE", Open. Mode. Binary, Open. Access. Write) ' Close before reopening in another mode. File. Close(1) • Microsoft. Visual. Basic namespace must be imported Compunet Corporation 4

File. Open Function • This code example opens the file in Output mode; any process can read or write to file. File. Open(1, "TESTFILE", Open. Mode. Output, Open. Shared) ' Close before reopening in another mode. File. Close(1) • Microsoft. Visual. Basic namespace must be imported Compunet Corporation 5

Editing Files Opened for Sequential Access • To edit a file, you must first read its contents to program variables, then change the variables, and finally, write the variables back to the file. • To read strings from files – Retrieve the contents of a text file by opening it for Input. – Use the Line. Input, Input. String, or Input functions to copy the file into program variables Compunet Corporation 6

Editing Files Opened for Sequential Access (Contd. ) Dim Lines. From. File, Next. Line As String Dim File. Num As Integer Do Until EOF(File. Num) Nextline = Line. Input(File. Num) Lines. From. File = Lines. From. File & Next. Line & Chr(13) & Chr(10) Loop • The Line. Input function recognizes the end of a line when it comes to the carriage return/line feed sequence Compunet Corporation 7

Editing Files Opened for Sequential Access (Contd. ) • You can use the Input. String function to copy a specified number of characters from a file to a variable, provided the variable is large enough. For example, the following code uses Input. String to copy Char. Count number of characters to a variable: Lines. From. File = Input. String(File. Num, Char. Count) Compunet Corporation 8

Editing Files Opened for Sequential Access • You can also use the Input function, which reads a list of numbers and/or string expressions from a file. For example, to read in a line from a mailing list file, you might use the following statements: Input(File. Num, fname) lname) street) city) state) zip) Compunet Corporation 9

Input Function Example • This example uses the Input function to read data from a file into two variables. This example assumes that TESTFILE is a file with a few lines of data written to it using the Write function; that is, each line contains a string in quotations and a number separated by a comma, for example, ("Hello", 234). File. Open(1, "TESTFILE", Open. Mode. Output) Write(1, "hello") Write(1, 14) File. Close(1) Dim s As String Dim i As Integer File. Open(1, "TESTFILE", Open. Mode. Input) Input(1, s) Debug. Write. Line(s) Input(1, i) Debug. Write. Line(i) File. Close(1) Compunet Corporation 10

Input. String function Example • This example uses the Input. String function to read one character at a time from a file and print it to the Output window. This example assumes that MYFILE is a text file with a few lines of sample data. Dim one. Char As Char File. Open(1, "MYFILE. TXT", Open. Mode. Input) ' Open file. While Not EOF(1) ' Loop until end of file. one. Char = (Input. String(1, 1)) ' Get one character. System. Console. Out. Write. Line (one. Char) End While File. Close(1) Compunet Corporation 11

Line. Input Function Example • This example uses the Line. Input function to read a line from a sequential file and assign it to a String variable. This example assumes that TESTFILE is a text file with a few lines of sample data. Dim Text. Line As String File. Open(1, "TESTFILE", Open. Mode. Input) ' Open file. While Not EOF(1) ' Loop until end of file. Text. Line = Line. Input(1) ' Read line into variable. Debug. Write. Line(Text. Line) ' Print to the console. End While File. Close(1) ' Close file Compunet Corporation 12

Writing Strings to Sequential. Access Files • You can add data in the form of strings to existing files via the Print Function or in the form of numbers and string expressions through the Write Function. • To write strings to files – Use the File. Open Function to open a text file for Output or Append. – Use the Print function to write the strings to the file as in the following example, which a text editor might use to copy the contents of a text box into a file: Print(File. Num, The. Box. Text) Compunet Corporation 13

Print, Print. Line Functions • Print will not include a linefeed at the end of a line; Print. Line, however, will include a linefeed. • Data written with Print is usually read from a file with Line. Input or Input • If you omit Output for Print. Line, a blank line is printed to the file; for Print, nothing is output. • For Boolean data, either True or False is printed • Date data is written to the file using the standard short date format recognized by your system. • Nothing is written to the file if Output data is empty. However, if Output list data is DBNull, Null is written to the file. • For Error data, the output appears as Error errorcode Compunet Corporation 14

Print, Print. Line Functions Example • This example uses the Print and Print. Line functions to write data to a file. File. Open(1, "c: trash. txt", Open. Mode. Output) Print(1, "This is a test. ") ' Print text to file. Print. Line(1) ' Print blank line to file. Print. Line(1, "Zone 1", TAB(), "Zone 2") Print. Line(1, "Hello", "World") ' Separate strings with a tab. Print. Line(1, SPC(5), "5 leading spaces ") Print. Line(1, TAB(10), "Hello") ' Print word at column 10. Dim a. Bool As Boolean Dim a. Date As Date. Time a. Bool = False a. Date = Date. Time. Parse("February 12, 1969") Print. Line(1, a. Bool, " is a Boolean value") Print. Line(1, a. Date, " is a date") Compunet Corporation 15 File. Close(1) ' Close file.

Write, Write. Line Functions • Writes data to a sequential file. Data written with Write is usually read from a file with Input. • If you omit Output, a blank line is printed to the file. Write. Line(1) ' Print blank line to file. • The Write function inserts commas between items and quotation marks around strings as they are written to the file Compunet Corporation 16

Write, Write. Line Functions (Contd. ) • Numeric data is always written using the period as the decimal separator. • For Boolean data, either #TRUE# or #FALSE# is printed. • For null data, #NULL# is written • For Error data, the output appears as #ERROR errorcode#. • Write. Line inserts a newline character (that is, a carriage return–linefeed, or Chr(13) + Chr(10)), after it has written the final character in Output to the file. Compunet Corporation 17

Write, Write. Line Functions (Contd. ) File. Open(1, "TESTFILE", Open. Mode. Output) Write(1, "This is a test. ") Write. Line(1) ' Print blank line to file. Write. Line(1, "Zone 1", TAB(), "Zone 2") Write. Line(1, "Hello", "World") Write. Line(1, SPC(5), "5 leading spaces ") Write. Line(1, TAB(10), "Hello") Dim a. Bool As Boolean Dim a. Date As Date. Time a. Bool = False a. Date = Date. Time. Parse("February 12, 1969") Write. Line(1, a. Bool, " is a Boolean value") Write. Line(1, a. Date, " is a date") File. Close(1) Compunet Corporation 18

Writing Text to a File Imports System. IO Class Test Public Shared Sub Main() ' Create an instance of Stream. Writer to write text to a file. Dim sw As Stream. Writer = New Stream. Writer("Test. File. txt") ' Add some text to the file. sw. Write("This is the ") sw. Write. Line("header for the file. ") sw. Write. Line("----------") ' Arbitrary objects can also be written to the file. sw. Write("The date is: ") sw. Write. Line(Date. Time. Now) sw. Close() End Sub End Class Compunet Corporation 19
- Slides: 19