Component 10a, Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 10 – Working with Binary Files.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
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.
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.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Guide To UNIX Using Linux Third Edition
ACSE th Conference The Iconic Programmer Stephen Chen.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Introduction to Shell Script Programming
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
Visual Basic: An Object Oriented Approach 5: Structured Programming.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Linux+ Guide to Linux Certification, Third Edition
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Indexed and Relative File Processing
Visual Basic.NET BASICS Lesson 5 Exponentiation, Order of Operations, and Error Handling.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
I Power Higher Computing Software Development Development Languages and Environments.
Chapter Three The UNIX Editors.
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.
VAT Calculator program Controls Properties Code Results.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Linux Administration Working with the BASH Shell.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 14: Sequential Access Files
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Variables and Arithmetic Operations
Chapter 2 - Introduction to C Programming
Topics Introduction to File Input and Output
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Fundamentals of visual basic
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 2 - Introduction to C Programming
Topics Introduction to File Input and Output
Introduction to C Programming
Presentation transcript:

Component 10a, Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 10 – Working with Binary Files – Error handling – Working with other applications

Component 10a, Slide 2 CP2030 Copyright © University of Wolverhampton Aims and objectives v Understand the structure of Binary Files v Use the various techniques available in Visual Basic to write to binary files. v Understand how to “launch” and interact with other applications from within V.B.

Component 10a, Slide 3 CP2030 Copyright © University of Wolverhampton VB data files v VB supports three kinds of data files – Sequential files store data as ACII text. Characters are read from and stored to the file character by character – Random-access files store data in special VB internal formats. Such files require rigid file structure – Binary files store data as individual bytes. No file structure exists, and no special internal formats are assumed v we will look at Binary files this week

Component 10a, Slide 4 CP2030 Copyright © University of Wolverhampton Binary files v Sequential and random access files have a certain degree of organisation of the data stored within them. Whereas VB treats binary files as just a file consisting of a stream of bytes v Treating files as binary files is useful to handle and interpret files created in formats alien to VB such as spreadsheets or wordprocessors, as you can read, or alter any bytes including ones that are control characters. v VB only sees the data as bytes and so any interpretation of what the each Byte means must be written into the program

Component 10a, Slide 5 CP2030 Copyright © University of Wolverhampton Three steps to process a Binary File Use the following three steps to process binary files 1 open the File For Binary 2 Use a record variables in Get and Put instructions to read and write the data 3 Close the file

Component 10a, Slide 6 CP2030 Copyright © University of Wolverhampton 1st step - opening the file The syntax is as follows: Open filename For BinaryAs #filenum where filename is the filename and path #filenum is the filehandle number e.g. applying this to a WORD document Open “c:\temp\letter.doc” For Binary As #1

Component 10a, Slide 7 CP2030 Copyright © University of Wolverhampton 2nd step - writing or reading to the file v We can use the Put statement to write to a binary file record and the Get statement to read from the file v It has the form Get #filenum, startbyte, variable Put #filenum, startbyte, variable Put #1, 12, iCount - startbyte is a numeric expression specifying the byte in the file where i/o begins - variable is any variable used to transmit or receive the data values

Component 10a, Slide 8 CP2030 Copyright © University of Wolverhampton File position pointer v A file position pointer is associated with each binary file v identifies which byte is to be written to or read from – first byte is position 1, 2nd is number 2 and so on v can be moved to any byte in the file then read or write as many bytes as you want v If we omit startbyte with Get or Put statements then the file position pointer establishes a default value, successive Get and Put instructions adjust this value appropriately

Component 10a, Slide 9 CP2030 Copyright © University of Wolverhampton Get and Put v Get reads the necessary number of Bytes to satisfy the length of the variable e.g 2 bytes for a single, 8 for a currency and so on. v For a fixed length string or a user defined type, Get reads the number of bytes to satisfy the length of the variable. A variable-length string variable will have the same number of bytes read into it as being stored in the variable at that time. v Put writes the number of bytes equal to the length of the variable

Component 10a, Slide 10 CP2030 Copyright © University of Wolverhampton 3rd step - closing the file As with all file handling work it is important to Close the file when all i/o activity has finished format Close #filenum e.g. Close #1

Component 10a, Slide 11 CP2030 Copyright © University of Wolverhampton Seek and Loc functions v With binary files Seek and Loc functions return a file position in number of bytes from the beginning of the file. Seek returns the current file pointer and Loc returns the number of the last byte read. E.g. Put #4, 26, iMyAge Print “Byte number file pointer is at is ” ; Seek (4) Print “Last byte written was number” ; Seek (4) which gives the output Byte number file pointer is at is 28 last byte written was number 27

Component 10a, Slide 12 CP2030 Copyright © University of Wolverhampton Error Handling - On Error v Probably the best way to find if a file exists is to use the ‘On Error’ statement, which catches all file related errors On Error { GoTo line | Resume Next | GoTo 0 } v Goto Line : is either a line number or a label – e.g. On Error Goto ErrorHandler: – e.g. On Error Goto 20 v Resume Next : causes next line of code to be executed, ignoring the error v Goto 0 : turns off the error handling

Component 10a, Slide 13 CP2030 Copyright © University of Wolverhampton Error Handling - Code Structure Sub subname declarations code not having error handling if any On Error Goto ErrorHandlingRoutineLabel code having error handling On Error Goto 0 ‘[Optional] turn off error handling, if required code not having error handling if any Exit Sub ErrorHandlingRoutineLabel: Error handling code Resume Next ‘Resume at next line after where the error occured End Sub

Component 10a, Slide 14 CP2030 Copyright © University of Wolverhampton Error Handling - Resume Statement v The Resume statement must be in the Error Handling routine or else an error occurs Resume { [0] | Next | line} v Resume 0 : Program execution resumes at the line causing the error v Resume Next : Program execution resumes at the line immediately after the one causing the error v Resume Line : Either a line number or label in the main procedure where code is to resume executing

Component 10a, Slide 15 CP2030 Copyright © University of Wolverhampton Error Handling - Example v Here when the file or drive is incorrect an error occurs Dim sMsg As String' Declare variables. On Error GoTo ErrorHandler' Set up error handler. Open “C:\CISFILE.TXT" For Input As #1' Try to open file. ‘statements Close #1' Close the file. Exit Sub' Exit before entering ErrorHandler:' Error handler Msgbox “Error in Opening File”,,”File Access Error” Resume Next' Resume procedure. End Sub

Component 10a, Slide 16 CP2030 Copyright © University of Wolverhampton Error Handling - Err and Error() v The ‘Err’ statement is the error number returned from the last error to occur v For full list see “Trappable Errors” on Help File errors in range 52-76; e.g. – 53 File not found – 61 Disk full Other common errors – 9 Subscript out of range – 11 Division by zero v The ‘Error( ErrorNumber ) statement returns a message describing the specified error number

Component 10a, Slide 17 CP2030 Copyright © University of Wolverhampton Improved Error Handling v An improved message for the message box in the error handling routine could then be ErrorHandler:' Error handler Msgbox Error(Err),,”File Access Error” Resume Next' Resume procedure. End Sub v Here the message could be ‘ file not found ’ or ‘ File already open’ etc.

Component 10a, Slide 18 CP2030 Copyright © University of Wolverhampton Error Handling, continued v For a comprehensive error handling section: ErrorHandler: Select Case Err Case 58: MsgBox Error(Err),,”File Access Error” …….. ‘Code to allow user to “browse”for filename …….. ‘ then open valid file. Case 61: MsgBox Error(Err),,”Replace with new disk” …….. End Select

Component 10a, Slide 19 CP2030 Copyright © University of Wolverhampton Use the power of existing applications…... Why write ‘Edit’ or ‘Print’ functionality into your VB program when Microsoft have already done it for you with NOTEPAD (or WORD). Similarly, why re-invent the ‘Spreadsheet’ ?? BUT…. several pitfalls…..requires some experimentation to get it right. Try it !…………………….....

Component 10a, Slide 20 CP2030 Copyright © University of Wolverhampton A simple way to use other applications … Shell ( commandstring [,windowstyle] ) eg Shell (NOTEPAD) or Shell (NOTEPAD, 2) Commandstring = name of executable, batchfile etc.exe assumed but could be.bat.com.pif not case sensitive, BUT must be EXACT name of package Shell starts package ASYNCHRONOUSLY…care!!

Component 10a, Slide 21 CP2030 Copyright © University of Wolverhampton windowstyle ….. 1 = Normal with focus 2 = Minimised with focus (default) etc….see help for full list.

Component 10a, Slide 22 CP2030 Copyright © University of Wolverhampton …but then you often give the shelled application the focus……. AppActivate applicationhandle eg AppActivate applicationhandle an example should help (see later)…………..

Component 10a, Slide 23 CP2030 Copyright © University of Wolverhampton When application running…SENDKEYS SendKeys keystring [, wait ] eg SendKeys “Computing” or SendKeys “ABC”, TRUE wait can be true or false (default=false). Specifies that execution of calling procedure will not continue UNTIL ‘other’ application/package has consumed these keys.

Component 10a, Slide 24 CP2030 Copyright © University of Wolverhampton ‘Special’ keys each have unique name eg {UP} is up arrow key, {ESC} is escape etc Note braces{}…….see help for full list Shifted keys are achieved by using ‘prefix’ codes, eg SendKeys +{ESC} sends Shift plus Escape Note… + = shift ^ = control %=alt etc

Component 10a, Slide 25 CP2030 Copyright © University of Wolverhampton Repeated keys are easy……… SendKeys {key repeats} eg Sendkeys {LEFT 40} sends 40 presses of Left arrow key

Component 10a, Slide 26 CP2030 Copyright © University of Wolverhampton A couple of examples…... nfile = File1.ListCount Rem assume your app. uses a FILELIST control crlf$ = Chr$(13) + Chr$(10) Rem put required text into clipboard Clipboard.Clear For a = 0 To nfile - 1 txt$ = File1.List(a) Clipboard.SetText (Clipboard.GetText() + crlf$ + txt$) Next a continued next slide………….

Component 10a, Slide 27 CP2030 Copyright © University of Wolverhampton txt$ = "Notepad" MyAppID = Shell(txt$, 1): Rem start NOTEPAD Rem crude way of giving it time to execute Rem DOEVENT would be better (see help) t = Timer: Do: Loop Until Timer > t Or Timer < 10 AppActivate MyAppID: Rem give NOTEPAD the focus SendKeys "%E": Rem send ALT+E ie select 'EDIT' menu SendKeys "P": Rem send 'P' ie 'PASTE' text currently in clipboard done.

Component 10a, Slide 28 CP2030 Copyright © University of Wolverhampton An example NOT using clipboard…. Rem start NOTEPAD with a specified ASCII file txt$ = "Notepad \MYFILE.TXT" MyAppID = Shell(txt$, 1) t = Timer: Do: Loop Until Timer > t Or Timer < 10 AppActivate MyAppID

Component 10a, Slide 29 CP2030 Copyright © University of Wolverhampton Summary v We have investigated – Binary files u the file structure of the binary file type u how we can map reading and writing to a binary file using the Get and Put statement u the use of the Seek and Loc functions to determine the present or last byte position of the file position pointer. – How to trap and handle errors – Interacting with other applications