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,

Slides:



Advertisements
Similar presentations
CSM18 FilesDr Terry Hinton1 VB - Persistence in software Information entered into a program is volatile Can switch computer off Power can be removed accidentally.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
Access - Project 1 l What Is a Database? –A Collection of Data –Organized in a manner to allow: »Access »Retrieval »Use of That Data.
Visual Basic: An Object Oriented Approach 9 - Persistence.
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
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Chapter 1- Visual Basic Schneider1 Chapter 1 An Introduction to Computers and Visual Basic.
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.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
With Windows 7 Comprehensive© 2012 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Windows 7 Comprehensive.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Chapter 7 Working with Files.
ASP.NET Programming with C# and SQL Server First Edition
Using the Select Case Statement and the MsgBox Function (Unit 8)
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
File System Interface. File Concept Access Methods Directory Structure File-System Mounting File Sharing (skip)‏ File Protection.
Indexed and Relative File Processing
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
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.
CE Operating Systems Lecture 17 File systems – interface and implementation.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
The purpose of a CPU is to process data Custom written software is created for a user to meet exact purpose Off the shelf software is developed by a software.
Chapter 1: Introduction to Computers and Programming
Chapter 14: Sequential Access Files
An Introduction to Computers and Visual Basic
Ch. 8 File Structures Sequential files. Text files. Indexed files.
Files.
An Introduction to Computers and Visual Basic
Chapter 1: Introduction to Computers and Programming
Python I/O.
Topics Introduction to File Input and Output
CIS16 Application Development Programming with Visual Basic
New Perspectives on Windows XP
Text / Serial / Sequential Files
Tutorial 9 Sequential Access Files and Printing
An Introduction to Computers and Visual Basic
Files Management – The interfacing
Chapter 16 File Management
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Saving and Accessing Data
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
Text / Serial / Sequential Files
Presentation transcript:

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, close, append, eof u Line based sequential file access u File system details  Current directory, relative path, absolute path u Common Dialog control

VB - Persistence in software u Information entered into a program is volatile  Can switch computer off  Power can be removed accidentally  Power-cuts can occur  Computer System can crash u We need to make data persist beyond the execution of a program

Files u In business administration, a file is used to keep paper documents together in an orderly way u In a computer, a file stores data in an order way  items are written to the file in strict sequence  items are read from the file in the same sequence u The medium used to hold files is non-volatile  data in a file persists when the computer is off

Computer file mechanisms u Programming languages provide commands to access files  open, read, write, append, close u The operating system maintains a link between a program and an open file using a file reference or handle:  An integer value by which the file can be identified

Program Files vs Data Files u A program file contains the instructions that both create the user interface and tell the objects how to respond to events  Also called an executable file u A data file is a collection of information of a specific category, or format

File Storage Hierarchy u A file system is a collection of files files u Files can be viewed as sets of records u Records are sets of fields u Fields are variable types File Record Field 1 Field 2 Field 3 Field 4 Field 5

Record-based File Types u Random  Fixed record length  Indexed records  Fast access  Indices may limit accessibility u Example  CD recording can go right to the song you want  Library card file tells you exactly where the book is u Sequential  Does not have to be fixed length  No indexing overhead  Easily transferable u Example  Cassette tape must role through the tape to find correct place

Sequential Access Data Files u Similar to a cassette tape in that each record in the file is both stored and retrieved in consecutive order u Advantage: easy to create u Disadvantage: you can process the records only in the order in which they were entered u Use for small files, files consisting only of text, or files of records that will always be accessed in sequential order

Open Statement u Open pathname For mode As # filenumber  pathname is the name of the file you want to open; it should include the drive letter and path  a String variable  a String literal, wrapped in quotes  mode can be either Input, Output, or Append  filenumber is a number that you assign to the file, and it must be an integer between 1 and 511, inclusive  could be an integer variable

Sequential Access File Modes u Input - you open a file for input when you want to read the file u Output - you open a file for output when you want to create a new file and then write data to it u Append - you open a file for append when you want to add data to the end of an existing file

Open File Pointer u When you open a file for input, the open file pointer is positioned at the beginning of the file, immediately before the first character u When you open a file for output, the open file pointer is positioned at the beginning of the empty file u When you open a file for append, the open file pointer is positioned immediately after the last character in the file

Write # Statement u Write # filenumber, [outputlist]  filenumber is the number used in the Open statement to open the file  outputlist is one or more numeric or string expressions, separated by commas

Close Statement u Close [# filenumber]  filenumber is the number used in the Open statement to open the file  A Close statement with no filenumber closes all open files

Code to create a file Sub CreateFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Output As #intFile strName = InputBox("Name?") Write #intFile, strName intNumber = val(InputBox("Number?")) Write #intFile, intNumber Close #intFile End Sub Sub CreateFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Output As #intFile strName = InputBox("Name?") Write #intFile, strName intNumber = val(InputBox("Number?")) Write #intFile, intNumber Close #intFile End Sub

Inspecting a Sequential Access File u Use a basic word processor or text editor u Each line in the sequential access file represents a record u Fields in the record are separated by commas u String fields are enclosed in quotation marks

EOF Function u EOF(filenumber)  filenumber is the number used in the Open statement to open the file  EOF stands for “end of file”  returns the Boolean value True if the record pointer is at the end of the file (after the last record); otherwise it returns the Boolean value False  Do While Not EOF(1)  Do Until EOF(1)

Input # Statement u Input # filenumber, variablelist  filenumber is the number used in the Open statement to open the file  variablelist is one or more numeric or string variables, separated by commas  each variable in the variablelist is associated with a field in the record  the number, data type, and order of the variables in the variablelist must match the fields in the record

Code to retrieve data from a file Sub ReadFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Input As #intFile Input #intFile, strName MsgBox strName Input #intFile, intNumber MsgBox intNumber Close #intFile End Sub Sub ReadFile( ) Dim intFile As Integer ' File handle Dim strName As String Dim intNumber As Integer intFile = FreeFile Open "MyFile.dat" For Input As #intFile Input #intFile, strName MsgBox strName Input #intFile, intNumber MsgBox intNumber Close #intFile End Sub

Another Code Example u Dim intFileNum As Integer Private Sub cmdSave_Click() intFileNum = FreeFile Open "X:\demo.txt" For Append As #intFileNum Write #intFileNum, txtIn(0).Text, Val(txtIn(1).Text), txtIn(2).Text Close #intFileNum End Sub

Line-based File I/O Line Input #intFileNumber, strLineOfText Print #intFileNumber, strLineOfText u For output, the line of text may need to be assembled from various components using string concatenation u Text Error: in the code example on page 307 "Print #1" should be "Print #intStudentFile"

Windows File System u Drive  A physical data storage device, such as a hard disk, floppy, or cd  Each drive is assigned a letter  On most systems, the primary hard drive is 'C', and the floppy drive is 'A'  In the CS labs, your personal data is stored on "I", a network drive u File  Files contain data

Windows File System u Directory  Directories are used to organize files  A directory can contain other directories, or files  A drive can also contain directories or files u Current directory  Whatever directory you are currently "in"  Initially, the current directory is the place VB is stored  You may not have permission to create files in this directory  You can find the current directory using the CurDir function

Typical Windows file system C: Program Files temp WindowsDocuments And Setting AdministratorAll UsersDefault User DesktopCookiesStart Menu Hello.txt

Absolute Pathnames u To use a file in VB, you need more than its name  To access a file in a directory other than the current one, you need to know its pathname u A pathname is an address that shows the file’s position in the file system u Absolute path names give a files location in relation to the top of the file system structure  All absolute path names begin with a drive letter  Also called full path names

What is the full path of each hello.txt? C: Program Files temp WindowsDocuments And Setting AdministratorAll UsersDefault User DesktopCookiesStart Menu Hello.txt

Common Dialog Box u Common Dialog Box – provides the standard MS application interface for choosing a file from the disk. Components Menu from Project Menu; choose MS Common Dialog Control u Icon placed on form, but is not seen in runtime u Properties  CancelError – what to do if no file selected  DialogTitle – hint to user at what file you want  Filter – what default file types to search for  InitDir – what directory to look in first