© 1999, by Que Education and Training, Chapter 9, pages 447-483 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
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.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Component 9, Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 9 working with text files and random.
VBA Modules, Functions, Variables, and Constants
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Program Design and Development
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Guide To UNIX Using Linux Third Edition
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.
JavaScript, Third Edition
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.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
CIS Computer Programming Logic
Chapter 1: Introduction to Computers and Programming.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Cis303a_chapt03-2a.ppt Range Overflow Fixed length of bits to hold numeric data Can hold a maximum positive number (unsigned) X X X X X X X X X X X X X.
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.
Array Processing.
Chapter 11 File Systems and Directories. 2 File Systems File: A named collection of related data. File system: The logical view that an operating system.
Data Structure & File Systems Hun Myoung Park, Ph.D., Public Management and Policy Analysis Program Graduate School of International Relations International.
File Handling and Control Break Logic. Objectives In this chapter, you will learn about: Computer files Writing a program that reads from and/or writes.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Input and Output. Announcements  Exam Next Wednesday –Next Monday: Review session.  Invited talk: –7:30 PM,Tuesday, Oct 28th. –Prof. Katherine Socha.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
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.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
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,
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.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
Chapter 14: Sequential Access Files
CHP - 9 File Structures.
TMF1414 Introduction to Programming
Introduction to Scripting
Topics Introduction to File Input and Output
T. Jumana Abu Shmais – AOU - Riyadh
Text / Serial / Sequential Files
Stata Basic Course Lab 2.
Topics Introduction to File Input and Output
Introduction to C Programming
Presentation transcript:

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Data File Hierarchy/Terminology n Folder or Directory i A container for 0 or more files accessible from the same location. n File i A collection of related records stored as a unit on external media. n Record or Row i A collection of related fields such as many pieces of information about a person or thing. n Field i A single (meaningful) piece of information about a person or thing. n Byte i Normally a collection of one or more characters that comprise a field (character ~ byte) n Bit i Commonly written as 0 or 1 as the smallest unit of computer data. i Generally 8 bits = 1 byte, yielding 256 combinations--ASCII code

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Data File Hierarchy Example n Folder or Directory i For class files called Courses n File i The file CPT250Grades.txt inside the Courses folder holds all class information for one course. n Record or Row i Each record in the grades file contains Student Name, Student ID, three test grades and ten project grades. n Field i Student ID is comprised of the last 5 digits of the social security number. n Byte i Each character of the ID requires one byte of storage. n Bit i The character “2” of the student ID has the ASCII code 50, which is written as , where each symbol is a bit.

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Physical Details of Files n Before you can write programs that use files, you need to get answers to the following: i Can records be accessed sequentially? i Can records be accessed arbitrarily? i Do all of the records have the same length? i How are the fields ordered on each record? i What is the data type of each field? i Is the length of each field the same? i How are the fields separated?

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Different Types of Files n Random-access data files i Files designed to facilitate future retrieval of individual records in an arbitrary order. i Retrieving a record requires determining its location u Hashing u Index (ISAM) i Thus, it is logically used like an array but data resides on disk n Binary data files i A machine-readable format that can contain almost anything i Unreadable if loaded in a word processor or text editor i Program files u Special type of binary file that the OS can interpret with the right OCXs, DLLs, EXEs.

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Different Types of Files n Sequential data files i Data elements stored and retrieved one after another, in order i Examples u Text files (Report-Record or Display-Formatted format) –Simple documents such as README files created with ASCII text editor like Notepad –Designed to be read, so there are no field delimiters –Carriage return and line feed mark the end of each line u Comma-separated value (CSV) files –Common means for transferring data across applications –Contains variable length records separated by commas –Certain types of data are surrounded by special characters –Carriage return and line feed mark the end of each line/record u Fixed-width data files –Commonly created from/for COBOL programs

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach General usage of Files 1)Assign a file number i To refer to file in subsequent file access statements 2) Open the file i Identify file name & path, purpose, reference number, and record length if random file 3)Read/Write the file (possibly within a loop) i Specify reference number, action, and data elements u Read retrieves data from the file into specified memory locations (variables) u Write copies data in specified memory locations (variables) to current location in file 4)Close the file i Specify reference number to release the number and the file

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Assigning a File Number n Two choices i Use a literal constant (between 1-511) i Use FreeFile function to have VB automatically assign an available reference number i Use caution when working with multiple files simultaneously n Incorrect Dim InNbr As Integer Dim OtNbr As Integer InNbr = FreeFile ' Returns 1. OtNbr = FreeFile ' Returns 1. Open "Input.dat" For Input As #InNbr Open "Output.dat" For Output As #OtNbr n Correct Dim InNbr As Integer Dim OtNbr As Integer InNbr = FreeFile ' Returns 1. Open "Input.dat" For Input As #InNbr OtNbr = FreeFile ' Returns 2. Open "Output.dat" For Output As #OtNbr

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Opening a File n Provide information about the file before any access Open pathname For mode {Access access} {lock} _ As #filenumber {Len = recordlength} n Elements of Open statement i pathname: a string that defines the file name with full path i mode: (purpose: input, output, append, random, binary) u Establishes record pointer at start of file for all modes but append which starts at end of file i access: specifies allowable operations (needed with random) i lock: specifies allowable operations by other processes (n/a) i filenumber: reference number used in subsequent read/write operations i recordlength: number of bytes in a record of a random access file or buffer size for sequential files

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Reading from a File n CSV file Input #filenumber, VariableList u Data read from current location of file pointer into corresponding variables u Each comma or end-of-line delimits each field, unless surrounded in quotations u Normally, records were created using the Write statement

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Reading from a File n Report (display-formatted) file Line Input #filenumber, VariableName u Read the next line from current location of file pointer into specified variable u Normally, records were created using the Print statement n Fixed record-length file Get #filenumber, {recordnumber}, VariableName u Read the next record from current location of file pointer into specified variable u Needed by fixed-record length files since there is no end- of-line character u Normally, records were created using the Put statement

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Detecting End-of-File n Use EOF function i Special function that detects when the end of file has been reached EOF(filenumber) n Use Data sentinel i Record that contains specific, fixed values, which, when read, signal the last record has been processed n Use unique first record that defines the number of records that follow i Atypical approach that allows counting loop to be used to read the fixed number of records n Error handler to detect read past end of file error

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Writing to a File n CSV file Write #filenumber, VariableList u Data in variable list written to current file location u Comma inserted between fields, carriage-return and line feed inserted at end of record, quotations placed around strings and # around Booleans and dates u Normally, records will be read using Input statement

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Writing to a File n Report (display-formatted) file Print #filenumber, OutputList u Print the output list to next line in file unless previous Print ended in comma or semicolon –May contain expressions, Spc(n), Tab(n), separated by comma or semicolon u Normally, records will be read using Line Input statement n Fixed record-length file Put #filenumber, {recordnumber}, VariableName u Write the data in VariableName to current file location u Normally, records will be retrieved using the Get statement

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Closing a File n Notifies the operating system that you are done with the file Close {{#}filenumberlist} i Examples u Close #EmpFile, #TaxFile u Close i For output files, the buffers are flushed to insure that everything gets written to the file. i When reading the same file more than once in a single session, you must open and close each time.

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach ‘ use static arrays and literal RefNbr Open “file” For Input As #1 Input #1, EndVal For Count = 1 To EndVal Step 1 Input #1, Array(Count) ‘ steps to process repeatedly Next Count Close #1 Reading Files with pre-data n When pre-data is included at start of data file or the program knows the number of records, use For/Next counting loop n Loop condition compares loop counter against the number of records read so far False True Count > EndVal? Set Count = 1 Count = Count + 1 Open file for input (#1) Input #1, EndVal Input #1, Array(Count) Other steps to process in loop Close #1

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach False True TempVar = Sentinel data Set Count = 0 Count = Count + 1 Open file for input (#1) Input #1, TempVar Other steps to process in loop Close #1 Input #1, TempVar Array(Count) = TempVar Reading Files with post-data data Pre-test loop to check for sentinel data ‘ use static arrays and literal RefNbr Open “file” For Input As #1 Input #1, TempVar Count = 0 Do Until TempVar = SentinelData Count = Count + 1 Array(Count) = TempVar ‘ steps to process repeatedly Input #1, TempVar Loop Close #1

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Reading Files with no pre/post data Pre-conditional loop to check EOF ‘ use static arrays and literal RefNbr Open “file” For Input As #1 Count = 0 Do Until EOF(1) Count = Count + 1 Input #1, Array(Count) ‘ steps to process repeatedly Loop Close #1 **This is the most likely approach when files are used False True EOF(1) Set Count = 0 Count = Count + 1 Open file for input (#1) Input #1, Array(Count) Other steps to process in loop Close #1

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach How does reading a CSV file work? n Open statement i Record pointer set to start of file (before first record) n Each time an input statement is processed i For each variable listed u Type of data in field must be compatible with the data type of the corresponding variable where it will be stored u Data at file’s record pointer is read and stored in corresponding, named field (variable) u Record pointer moves to the start of the next field (whether on same line or next line) n Close statement to release file to OS

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach VB Statements to Read CSV File ' Use dynamic array and FreeFile function Dim EmpCount As Integer Dim CsvInFile As Integer CsvInFile = FreeFile ' Returns 1. Open “a:\sample.csv” For Input As #CsvInFile Do Until EOF(CsvInFile) EmpCount = UBound(fEmp) + 1 ReDim Preserve fEmp(0 To EmpCount) As EmpType Input #CsvInFile, fEmp(EmpCount).ID, _ fEmp(EmpCount).Name, fEmp(EmpCount).Rate, _ fEmp(EmpCount).Hours ' optional code to process employee’s data Loop Close #CsvInFile

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach How does writing a CSV file work? n Open statement i New file created in stated path with given name i Output mode u Record pointer is set to the start of the file i Append mode u Record pointer is set to the end of the file n Each time a write statement is processed i For each variable listed u Data in memory (variable) is written at record pointer –Strings enclosed in double quotes & Boolean and dates enclosed in # –Comma separator used unless last variable in list i Carriage return-line feed mark the end of the line n Close statement to release file to OS

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach How does reading a report file work? n Open statement i same as writing CSV file n Each time a print statement is processed i Start on a new line unless the previous print ended in a comma “,” or semicolon “;” i For each expression listed u Data is written at current file pointer –Expressions separated by semicolons “;” keep file pointer where it left off –Expressions separated by commas “,” move pointer to next print zone n Close statement to release file to OS

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach VB Statements to Write CSV File ' Use dynamic array and FreeFile function Dim Index As Integer Dim EmpCount As Integer Dim CsvOutFile As Integer CsvOutFile = FreeFile ' Returns 1. Open “a:\sample.csv” For Output As #CsvOutFile EmpCount = UBound(fEmp) For Index = 1 To EmpCount Step 1 Write #CsvOutFile, fEmp(EmpCount).ID, _ fEmp(EmpCount).Name, fEmp(EmpCount).Rate, _ fEmp(EmpCount).Hours Next Index Close #CsvOutFile

© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Before using Sequential Files n You must determine or plan its structure i Number of records u To determine type of loop processing & size of array(s) needed i Number, order, and type of fields per record u To determine the fields (generally on one line) –if different types: for the user-defined array –if same type: number of columns for 2-d array i Pre-data u Generally the number of following records (counting loop) i Post-data (Sentinel data) u If present, to flag when the last good data set has been reached i If no pre-data or post-data, may use EOF function or write error handler to determine when end of file reached