INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.

Slides:



Advertisements
Similar presentations
File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
Advertisements

CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
Component 9, Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 9 working with text files and random.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 9: Sequential Access Files and Printing
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
2015/6/301 TransCAD Managing Data Tables. 2015/6/302 Create a New Table.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Visual C Sharp – File I/O - 1 Variables and arrays only temporary - lost when a program terminates Files used for long term storage (Data bases considered.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #4: Working with Variables and User Interfaces IE 212: Computational Methods for Industrial Engineering.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Chapter 8 Data File Basics.
File I/O 11_file_processing.ppt
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.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 9 I/O Streams and Data Files
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
5 1 Data Files CGI/Perl Programming By Diane Zak.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
Loops and Files. 5.1 The Increment and Decrement Operators.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
Error Trapping Exception Catching 1. Syntax & Compile-time  VB – Editor/Compiler Logic and Design  You Runtime  You Types of Errors: 2 When your program.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 Chapter 4 Program Input and the Software Design Process.
Learners Support Publications Working with Files.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
1 Record Structured Data. We now look at actual record structured data. Using our dimensions example: struct name { membertype membername; }; struct.
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.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
INPUT AND OUTPUT.
Chapter 7 Text Input/Output Objectives
Introduction to C Topics Compilation Using the gcc Compiler
Ch 10 Sequential Access Files
Files and Streams Lect3 CT1411.
Chapter 3.5 Input and Output
Control Structures Part B - Message and Input Boxes
Files and Streams Lect10 GC201 12/1/2015.
CIS16 Application Development and Programming using Visual Basic.net
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
BO65: PROGRAMMING WRITING TO TEXT FILES.
Text / Serial / Sequential Files
Presentation transcript:

INPUT AND OUTPUT 1

OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2

MsgBox MsgBox (text [,buttons [, caption/title]]) As MsgBoxResult MsgBox(“You must provide a file name”,,”File Error”) 3 To learn more about MsgBoxes, use Help.Help

response = MsgBox(“Incorrect name-Continue?”, 4+32,”File Error”) MsgBox 4 CType(4+32,MsgBoxStyle),”File Error”) MsgBox (text [,buttons [, caption/title]]) As MsgBoxResult When casting using CInt, we know the result will be an Integer. However, when using CType, the type is listed as the last parameter. When casting using CInt, we know the result will be an Integer. However, when using CType, the type is listed as the last parameter. Using numerical values is rather cryptic.

response = MsgBox(“Incorrect name-Continue?", y, "File Error")) MsgBox Instead of using the numerical values, we will use VB constants: 5 MsgBoxStyle.YesNo,"File Error") response = MsgBox(“Incorrect name-Continue?”, CType(4+32,MsgBoxStyle),”File Error”) MsgBoxStyle.YesNo+,"File Error") MsgBoxStyle.YesNo+MsgBoxStyle.Question, "File Error") Ctype(MsgBoxStyle.YesNo+MsgBoxStyle.Question, MsgBoxStyle),"File Error") Now let’s look at the value for response.

MsgBox 6 response = MsgBox(“Incorrect name-Continue?”, CType(MsgBoxStyle.YesNo+MsgBoxStyle.Question, MsgBoxStyle),"File Error") If(response = 7) Then... vbNo) Then... Now we have some code that is more understandable—Yes and No buttons, a Question mark icon, and we are checking for a negative response.

MessageBox MessageBox.Show (text [,caption/title [,buttons [, icon[,defaultbutton]]]]) MessageBox.Show("You must provide a file name", "File Error",MessageBoxButtons.OK) To learn more about MessageBoxes, use Help or see pages in the text.Help 7

INPUT Textboxes InputBox Function/Method Files (next topic) 8

InputBox InputBox ( Prompt As String[, Title As String][, DefaultResponse As String][, XPos As Integer][, YPos As Integer]) As String fileName = InputBox("You must provide a file name", "File Error") To learn more about InputBoxes, use Help.Help 9 If the user clicks OK, fileName will be grades. xlsx, and if the user clicks Cancel, fileName will be the empty string.

Accessing Files 10

Access Methods 1.Sequential Access Method (SAM) 2.Direct (Random) Access Method (DAM) 3.Indexed Sequential Access Method (ISAM) 11

Accessing Files 1.We are going to limit our work with files and only use the Sequential Access Method (SAM) 2.Text Files (which we will discuss) 3.Binary Files (which we will not discuss) 12

1.Include necessary libraries 2.Declare a file stream object 3.Open the file stream 4.Transfer the data 5.Close the file stream 13 Steps needed to transfer data to/from files

14 Before we can transfer data from a text file, we have to know how it is arranged. For our first example, the data are listed one measurement per line. SAM with Text Files Using the FileStream Class When accessing a file, if no path is specified, then the file must be in the debug subfolder.

C++ #include ifstream inFile; inFile.open(“measures.txt”); inFile.close(); VB Imports System.IO Dim inFile As StreamReader inFile = File.OpenText (“measures.txt”) inFile.close() 15 I/O is one of the areas in which c and c++ are different! This will cause an error if the file does not exist. We will address trapping errors or catching exceptions in the next slide show.

C++ #include ofstream outFile; outFile.open(“results.txt”); outFile.close(); VB Imports System.IO Dim outFile As StreamWriter outFile = File.CreateText (“results.txt”) outFile.close() 16 To add to an existing file, you would use File.AppendText

17 Let’s put the two previous slides together and create a program the reads data from a file, performs a calculation, and writes the result to another file.

C++ #include ifstream inFile; inFile.open(“measures.txt”); ofstream outFile; outFile.open(“results.txt”);. inFile.close(); outFile.close(); VB Imports System.IO Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.txt”) outFile = File.CreateText (“results.txt”). inFile.close() outFile.close() 18 inFile >> length; length = Cint(inFile.Readline) inFile >> width; width = Cint(inFile.ReadLine) area=length*width; area = length * width outFile << area << endl; outFile.WriteLine(area.ToString) Assume that we have: int length;Dim length As Integer int width;Dim width As Integer int area;Dim area As Integer Assume that we have: int length;Dim length As Integer int width;Dim width As Integer int area;Dim area As Integer Why are these needed? NOT needed, the Write and WriteLine methods are “overloaded.” What other code must we add to this program?

19 Although we could use a For-Loop why is this inappropriate? Imports System.IO. Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.txt”) outFile = File.CreateText (“results.txt”) inFile.close() outFile.close() length = Cint(infile.Readline) width = Cint(infile.ReadLine) area = length * width outFile.WriteLine(area) Do While (inFile.Peek <> -1) Loop Assume that we have: Dim i As Integer Assume that we have: Dim i As Integer For i = 1 To 3 Next i

20 How Are Imported Text Files Delimited?

21 Although the previous example works, I think of the input file as: Let’s look at the code necessary to use a CSV file. and the output file looking like: Unlike the text, I am using comma delimiters. Such files are referred to as CSV – Comma Separated Values. Space delimited Tab delimited Comma delimited If we were using a data file containing name address city state zip, why would this be a poor choice for a delimiter? If we were using a data file containing name address city state zip, why would this be a poor choice for a delimiter?

The Split method is defined on page 281and returns an array of strings, where each element contains a substring of the original string Split (split character) … name.Split (CChr(" ")) 22 Before writing the code we need to examine a method and a function Strings are usually stored with a string count header and then the string. For example “Tony” is stored as 4 T o n y and “Z” is stored as 1 Z. The character Z is stored as just Z. Consequently, we must cast the string into a character. In a like manner, if we are using a space “ “, we must cast it into a character. Strings are usually stored with a string count header and then the string. For example “Tony” is stored as 4 T o n y and “Z” is stored as 1 Z. The character Z is stored as just Z. Consequently, we must cast the string into a character. In a like manner, if we are using a space “ “, we must cast it into a character.

23 Consider the following example: Dim name, first, middle, last As String fields = name.Split(CChar(" ")) name first middle last Anthony Joseph Nowakowski name = "Anthony Joseph Nowakowski“ Dim fields(2) As String fields Anthony Joseph Nowakowski first = fields(0) Anthony Joseph Nowakowski middle = fields(1) last = fields(2)

The Split function is defined on page 291 and returns an array of strings, where each element contains a substring of the original string Split (string [,delimiterString] [,limit]) … Split(name) 24 Before writing the code we need to examine a method and a function If the delimiter is omitted, then a space is used. If the limit is omitted then, all substrings are returned. If the delimiter is omitted, then a space is used. If the limit is omitted then, all substrings are returned.

25 Before writing the code we need to examine a method and a function Consider the following example: Dim name, first, middle, last As String Dim fields(2) As String name = "Anthony Joseph Nowakowski" fields = Split(name) first = fields(0) middle = fields(1) last = fields(2)

26 We now modify the code for the CSV example Imports System.IO. Dim record, fields(1) As String Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.csv”) outFile = File.CreateText (“results.csv”) Do While (inFile.Peek <> -1) record = infile.ReadLine fields = Split(record,”,”) length = Cint(fields(0)) width = Cint(fields(1)) area = length * width outFile.Write(length & ”,”) outFile.Write(width & ”,”) outFile.WriteLine(area) Loop inFile.close() outFile.close() These create a new CSV file.

Using the “traditional” format. i.e. paragraphs indented and no double- spacing between paragraphs. 27 Our final text example involves the Gettysburg Address

28 Our final text example involves the Gettysburg Address Imports System.IO. Dim inFile As StreamReader inFile = File.OpenText("GBA.txt") inFile.Close() txtGB.Text = inFile.ReadToEnd Text Box or Label? lblGB.Text = inFile.ReadToEnd

29 Some Other Considerations What happens if we used the newer format? i.e. no indentation with double-spaced paragraphs. Instead of using ReadToEnd, what would happen if we used ReadLine to read each paragraph? –The tabbed format is easier to implement.

Read the Description Of Assigment-4 30