Text / Serial / Sequential Files

Slides:



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

30/04/ Selection Nested If structures & Complex Multiple Conditions.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
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.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
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
08/09/ Arrays Defining, Declaring & Processing.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
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.
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.
Chapter 10: Structures and Sequential Access Files
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
30/10/ Iteration Loops Do While (condition is true) … Loop.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
22/11/ Selection If selection construct.
Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
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,
31/01/ Selection If selection construct.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Unit 2 Technology Systems
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Introduction to Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Retrieving information from forms
Starter Write a program that asks the user if it is raining today.
File Handling Programming Guides.
Topics Introduction to File Input and Output
Number and String Operations
Learning to Program in Python
If selection construct
Do While (condition is true) … Loop
4.1 Strings ASCII & Processing Strings with the Functions
Visual Basic 6 Programming.
If selection construct
Do … Loop Until (condition is true)
1D Arrays Defining, Declaring & Processing
Text / Serial / Sequential Files
3.1 Iteration Loops For … To … Next 18/01/2019.
Nested Loops & The Step Parameter
Additional Topics in VB.NET
Random Access Files / Direct Access Files
JavaScript.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
CIS16 Application Development and Programming using Visual Basic.net
Using screens and adding two numbers - addda.cbl
BO65: PROGRAMMING WRITING TO TEXT FILES.
To understand what arrays are and how to use them
Just Basic Lessons 9 Mr. Kalmes.
Unit 3: Variables in Java
Just Basic Lessons Mr. Kalmes.
Topics Introduction to File Input and Output
10.3 Procedures Function Procedures 07/06/2019.
3.2 Working with Data Scope of variables 29/07/2019.
8 Records 25/07/2019.
4.4 – List vs Array Exercise 4.1: Array Variables
Presentation transcript:

Text / Serial / Sequential Files 12/07/2019

Learning Objectives Explain what a file does and why a file is useful. State the restrictions of a text file. Give the general form of code used to work with files. 12/07/2019

A File Stores data permanently: In all programs so far, any stored data has been lost when the program closes, and has to be entered again when the program next runs. When you have a lot of data to store like in ‘Program 9 Football Team Players’ this is obviously not feasible. 12/07/2019

Text Files Store data as characters represented by their ASCII codes. American Standard Code for Information Interchange Upper case letters (‘A’ - ‘Z’) 65 - 90. Lower case letters (‘a’ – ‘z’) 97 - 122. Numeric digits (‘0’ – ‘9’) 48 - 57. The space character is 32. 12/07/2019

Text / Serial / Sequential Files Have no natural built in structure to store records since data is simply stored as a sequence of characters. You can open a file to be read from or written to but not both at the same time. Data is read from or written to sequentially: Text files are Sequential files. 12/07/2019

Text / Serial / Sequential Access Restrictions When you read data you have to read all the data from the beginning to the end. When you add data you can only add on the end i.e. append or clear all data and start again. Not really possible to change the data in a text file. 12/07/2019

Opening a Text / Serial / Sequential file You have to open a file before you can use it. FileOpen(1, FileName, OpenMode.Append) FileOpen(1, FileName, OpenMode.Input) FileOpen(1, FileName, OpenMode.Output) Reference number of the file used by VB to identify it. You only need to use a higher number if you wish to open more than one file at a time. Append Write to the end of the file. Input Read from the file. Output Write to the file (clears all previous data). 12/07/2019

Filename The full path and name of the file. It is best to use a variable to hold all this: Dim FileName As String = CurDir() & "\....txt" Name of file.txt Finds the path of the current program, this will store the file in the same folder. It is best to do this otherwise every time you move the program you would have to change the path accordingly. 12/07/2019

Writing to a Text / Serial / Sequential file WriteLine(1, …) Puts quotation marks (“…”) around each string but not numbers, so treats numbers as separate items. Data to be written (on one line). Can add more data by , …, …, … Ref no. PrintLine(1, …) Puts spaces between data and the whole record is treated as one item. 12/07/2019

Reading from a Text / Serial / Sequential file VB uses an imaginary pointer when it reads through a file. When a text file is opened it points to the 1st line. 12/07/2019

Reading from a Text / Serial / Sequential file Input(1, …) Reads single data items, item by item. Imaginary pointer moves from item to item. … = LineInput(1) Reads lines of data, line by line. Imaginary pointer moves from line to line Ref no. Variable to which data is assigned. 12/07/2019

Reading from a Text / Serial / Sequential file Input or LineInput should be contained within a loop (to ensure that you don’t go read past the end of a file and force an error). Do While Not EOF(1) Input(1, …) Or … = LineInput(1) Rest of loop body statements. Loop EOF = End Of File This will loop through the whole file.

Closing a file FileClose(1) Ref no. 12/07/2019

Program 9.1 Text / Serial / Sequential File Specification: Allow the user to enter people’s names and ages and store these in a text file. Allow the user to display the contents of this file at any time and count the number of adults on file (18 or over). 12/07/2019

Program 9.1 Text / Serial / Sequential File txtName txtAge butAddToFile lstFile butDisplayFile butClearFile 12/07/2019

Program 9.1 Text / Serial / Sequential File ‘Declare a global variable to hold the name and path of the file. Dim FileName As String = CurDir() & "\NamesAndAges.txt" 12/07/2019

Program 9.1 Text / Serial / Sequential File butAddToFile: ‘Check if the age entered is a number. If IsNumeric(txtAge.Text) = False Then MsgBox("You must enter a number!") txtAge.Clear() txtAge.Focus() Exit Sub End If Dim Name As String Dim Age As Integer Name = txtName.Text Age = txtAge.Text FileOpen(1, FileName, OpenMode.Append) ‘Open the file to add new data. WriteLine(1, Name, Age) ‘Add the name and age to the file. FileClose(1) ‘Close the file. txtName.Clear() txtName.Focus() 12/07/2019

Program 9.1 Text / Serial / Sequential File butDisplayFile: Dim Name As String = “” Dim Age As Integer Dim NumberOfAdults As Integer lstFile.Items.Clear() FileOpen(1, FileName, OpenMode.Input) ‘Open the file for reading. Do While Not EOF(1) ‘Loop through the file to the end. Input (1, Name) ‘Read the name. Input (1, Age) ‘Read the Age. lstFile.Items.Add(Name & “ ” & Age) ‘Add name and age to the list box. If Age >= 18 Then ‘Count the number of adults. NumberOfAdults +=1 End If Loop FileClose(1) ‘Close the file. ‘Output the number older than 18 at the end of the list. lstFile.Items.Add("Number of adults: " & NumberOfAdults) 12/07/2019

Program 9.1 Text / Serial / Sequential File butClearFile: ‘Clear the file. FileOpen(1, FileName, OpenMode.Output) FileClose(1) ‘Close the file. lstFile.Items.Clear 12/07/2019

Program 9.1 Text / Serial / Sequential File Run the program and test it. Do you know why we have not used Print for writing to or LineInput to read from the file? Try using Print for writing to and LineInput to read from the file to see what happens if you are not sure. Why does the program crash? Just to make sure you change back when you are done. 12/07/2019

Commenting on Files In presentations 9.1 – 9.2 I will only ask for comments to files. Your comments MUST explain: What is the file for? And when it is being used: What are you doing, storing or retrieving? When (after and before what) are you doing this and why does it have to be done there? When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)? 12/07/2019

Extension Anna wants to find out about her fellow students’ reading habits. It will be part of her Literature coursework. She will ask questions online, so starts by designing a screen layout. The first four questions will ask for: student’s first name date of birth type of book they prefer (printed, audio-book or e-book) whether student reads novels (yes/no) The responses from each student will be stored as a record consisting of the following fields: FirstName DateOfBirth BookType ReadsNovels Anna is to write a program to analyse the responses. Write a program to input the responses and then to calculate the totals for each BookType (printed, audio-book or e-book). 12/07/2019

Plenary Explain what a file does and why a file is useful. Store data permanently. So data does need to be re-entered every time the program starts. 12/07/2019

Plenary What are the restrictions of a text file? 12/07/2019

Text Files Have no natural built in structure to store records since data is simply stored as a sequence of characters. You can open a file to be read from or written to but not both at the same time. Data is read from or written to sequentially: Text files are Sequential files. 12/07/2019

Sequential Access Restrictions When you read data you have to read all the data from the beginning to the end. When you add data you can only add on the end i.e. append or clear all data and start again. Not really possible to change the data in a text file. 12/07/2019

Plenary What is the general form of code used to work with files? Dim FileName As String = My.Application.Info.DirectoryPath _ & "\....txt" FileOpen(1, FileName, OpenMode.Input/Append/Output) Do While Not EOF(1) WriteLine(1, …) / PrintLine(1,…) … = LineInput(1) / Input(1, …) Rest of Loop Body Loop FileClose(1) 12/07/2019

Homework Write a program to store all the names (first and last) of students studying AS Computing in a text file. A user should be able to add more students to the list if they wish to. Use either PrintLine or WriteLine. A user should be able to click a button to see the list in a list box. 12/07/2019