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

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
File Access, Dialog Boxes, Error Handling, and Menus Chapter 5.
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.
Chapter 9: Sequential Access Files and Printing
File Input and Output Sequential Access Files. Format Function Syntax: –Format( expression, “format” ) Example: lblNumber.Caption = “You owe ” & Format(dblSum,
Chapter 2: Introduction to C++.
Chapter 8 Arrays and Strings
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Variables and Constants
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Tutorial 61 List Box Control Can be used to display a set of choices from which the user can select only one You also can create multi-selection list boxes.
Chapter 8 Arrays and Strings
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Creating Sequential Files, Adding Data, & Deleting Data.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
READING AND WRITING FILES. READING AND WRITING FILES SEQUENTIALLY  Two ways to read and write files  Sequentially and RA (Random Access  Sequential.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
Chapter 15 Strings as Character Arrays
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
Chapter 14: Sequential Access Files
INPUT AND OUTPUT.
Computing Fundamentals
Files and Streams Lect3 CT1411.
Topics Introduction to File Input and Output
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3.5 Input and Output
Text / Serial / Sequential Files
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
Additional Topics in VB.NET
Chapter 2: Introduction to C++.
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
Visual Basic and File Streaming
Topics Introduction to File Input and Output
Text / Serial / Sequential Files
OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)
Presentation transcript:

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

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)

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

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

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

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

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

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)

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)

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)

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)

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

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)

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

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.

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

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.

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)

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