190 likes | 214 Views
Programming with Visual Basic .NET. Input and Output Files Lecture # 6 Tariq Ibn Aziz. 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
E N D
Programming with Visual Basic .NET Input and Output Files Lecture # 6 Tariq Ibn Aziz Compunet Corporation
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 FileOpen (FileNumber, FileName, OpenMode.Input) Compunet Corporation
FileOpen Function • You must open a file before any I/O operation can be performed on it FileOpen(1, "TESTFILE", OpenMode.Input) ' Close before reopening in another mode. FileClose(1) • Microsoft.VisualBasic namespace must be imported File Mode File Number File Name Compunet Corporation
FileOpen Function • This example opens the file in Binary mode for writing operations only. FileOpen(1, "TESTFILE", OpenMode.Binary, OpenAccess.Write) ' Close before reopening in another mode. FileClose(1) • Microsoft.VisualBasic namespace must be imported Compunet Corporation
FileOpen Function • This code example opens the file in Output mode; any process can read or write to file. FileOpen(1, "TESTFILE", OpenMode.Output, OpenShare.Shared) ' Close before reopening in another mode. FileClose(1) • Microsoft.VisualBasic namespace must be imported Compunet Corporation
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 LineInput, InputString, or Input functions to copy the file into program variables Compunet Corporation
Editing Files Opened for Sequential Access (Contd.) Dim LinesFromFile, NextLine AsString Dim FileNum AsInteger DoUntil EOF(FileNum) Nextline = LineInput(FileNum) LinesFromFile = LinesFromFile & NextLine & Chr(13) & Chr(10) Loop • The LineInput function recognizes the end of a line when it comes to the carriage return/line feed sequence Compunet Corporation
Editing Files Opened for Sequential Access (Contd.) • You can use the InputString 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 InputString to copy CharCount number of characters to a variable: LinesFromFile = InputString(FileNum, CharCount) Compunet Corporation
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(FileNum, fname) Input(FileNum, lname) Input(FileNum, street) Input(FileNum, city) Input(FileNum, state) Input(FileNum, zip) Compunet Corporation
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). FileOpen(1, "TESTFILE", OpenMode.Output) Write(1, "hello") Write(1, 14) FileClose(1) Dim s AsString Dim i AsInteger FileOpen(1, "TESTFILE", OpenMode.Input) Input(1, s) Debug.WriteLine(s) Input(1, i) Debug.WriteLine(i) FileClose(1) Compunet Corporation
InputString function Example • This example uses the InputString 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 oneChar AsChar FileOpen(1, "MYFILE.TXT", OpenMode.Input) ' Open file. WhileNot EOF(1) ' Loop until end of file. oneChar = (InputString(1, 1)) ' Get one character. System.Console.Out.WriteLine (oneChar) EndWhile FileClose(1) Compunet Corporation
LineInput Function Example • This example uses the LineInput 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 TextLine AsString FileOpen(1, "TESTFILE", OpenMode.Input) ' Open file. WhileNot EOF(1) ' Loop until end of file. TextLine = LineInput(1) ' Read line into variable. Debug.WriteLine(TextLine) ' Print to the console. EndWhile FileClose(1) ' Close file Compunet Corporation
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 FileOpen 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(FileNum, TheBox.Text) Compunet Corporation
Print, PrintLine Functions • Print will not include a linefeed at the end of a line; PrintLine, however, will include a linefeed. • Data written with Print is usually read from a file with LineInput or Input • If you omit Output for PrintLine, 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
Print, PrintLine Functions Example • This example uses the Print and PrintLine functions to write data to a file. FileOpen(1, "c:\trash.txt", OpenMode.Output) Print(1, "This is a test.") ' Print text to file. PrintLine(1) ' Print blank line to file. PrintLine(1, "Zone 1", TAB(), "Zone 2") PrintLine(1, "Hello", "World") ' Separate strings with a tab. PrintLine(1, SPC(5), "5 leading spaces ") PrintLine(1, TAB(10), "Hello") ' Print word at column 10. Dim aBool AsBoolean Dim aDate AsDateTime aBool = False aDate = DateTime.Parse("February 12, 1969") PrintLine(1, aBool, " is a Boolean value") PrintLine(1, aDate, " is a date") FileClose(1) ' Close file. Compunet Corporation
Write, WriteLine 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. WriteLine(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
Write, WriteLine 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#. • WriteLine 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
Write, WriteLine Functions (Contd.) FileOpen(1, "TESTFILE", OpenMode.Output) Write(1, "This is a test.") WriteLine(1) ' Print blank line to file. WriteLine(1, "Zone 1", TAB(), "Zone 2") WriteLine(1, "Hello", " ", "World") WriteLine(1, SPC(5), "5 leading spaces ") WriteLine(1, TAB(10), "Hello") Dim aBool AsBoolean Dim aDate AsDateTime aBool = False aDate = DateTime.Parse("February 12, 1969") WriteLine(1, aBool, " is a Boolean value") WriteLine(1, aDate, " is a date") FileClose(1) Compunet Corporation
Writing Text to a File Imports System Imports System.IO Class Test PublicSharedSub Main() ' Create an instance of StreamWriter to write text to a file. Dim sw As StreamWriter = New StreamWriter("TestFile.txt") ' Add some text to the file. sw.Write("This is the ") sw.WriteLine("header for the file.") sw.WriteLine("-------------------") ' Arbitrary objects can also be written to the file. sw.Write("The date is: ") sw.WriteLine(DateTime.Now) sw.Close() EndSub EndClass Compunet Corporation