Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz."— Presentation transcript:

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

2 Compunet Corporation2 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)

3 Compunet Corporation3 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 Number File Name File Mode

4 Compunet Corporation4 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

5 Compunet Corporation5 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

6 Compunet Corporation6 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

7 Compunet Corporation7 Editing Files Opened for Sequential Access (Contd.) Dim LinesFromFile, NextLine As String Dim FileNum As Integer Do Until 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

8 Compunet Corporation8 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)

9 Compunet Corporation9 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)

10 Compunet Corporation10 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 As String Dim i As Integer FileOpen(1, "TESTFILE", OpenMode.Input) Input(1, s) Debug.WriteLine(s) Input(1, i) Debug.WriteLine(i) FileClose(1)

11 Compunet Corporation11 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 As Char FileOpen(1, "MYFILE.TXT", OpenMode.Input) ' Open file. While Not EOF(1) ' Loop until end of file. oneChar = (InputString(1, 1)) ' Get one character. System.Console.Out.WriteLine (oneChar) End While FileClose(1)

12 Compunet Corporation12 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 As String FileOpen(1, "TESTFILE", OpenMode.Input) ' Open file. While Not EOF(1) ' Loop until end of file. TextLine = LineInput(1) ' Read line into variable. Debug.WriteLine(TextLine) ' Print to the console. End While FileClose(1) ' Close file

13 Compunet Corporation13 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)

14 Compunet Corporation14 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

15 Compunet Corporation15 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 As Boolean Dim aDate As DateTime 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.

16 Compunet Corporation16 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

17 Compunet Corporation17 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.

18 Compunet Corporation18 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 As Boolean Dim aDate As DateTime aBool = False aDate = DateTime.Parse("February 12, 1969") WriteLine(1, aBool, " is a Boolean value") WriteLine(1, aDate, " is a date") FileClose(1)

19 Compunet Corporation19 Writing Text to a File Imports System Imports System.IO Class Test Public Shared Sub 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() End Sub End Class


Download ppt "Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz."

Similar presentations


Ads by Google