7 1 Files Text Serial Sequential Files 24122021

  • Slides: 28
Download presentation
7. 1 Files Text / Serial / Sequential Files 24/12/2021 1

7. 1 Files Text / Serial / Sequential Files 24/12/2021 1

Learning Objectives Explain what a file does and why a file is useful. State

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. Use the Date. Diff function. 24/12/2021 2

A File Stores data permanently: In all programs so far, any stored data has

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. n When you have a lot of data to store like this is obviously not feasible. n 24/12/2021 3

Text Files Store data as characters represented by their ASCII codes. n American Standard

Text Files Store data as characters represented by their ASCII codes. n 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. 24/12/2021 4

Text / Serial / Sequential Files Have no natural built in structure to store

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: n Text files are Sequential files. 24/12/2021 5

Text / Serial / Sequential Access Restrictions When you read data you have to

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 n add on the end i. e. append n clear all data and start again. or Not really possible to change the data in a text file. 24/12/2021 6

Opening a Text / Serial / Sequential file You have to open a file

Opening a Text / Serial / Sequential file You have to open a file before you can use it. n File. Open(1, File. Name, Open. Mode. Append) File. Open(1, File. Name, Open. Mode. Input) n File. Open(1, File. Name, Open. Mode. Output) n 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. 24/12/2021 Append Write to the end of the file. Input Read from the file. Output Write to the file (clears all previous data). 7

Filename The full path and name of the file. It is best to use

Filename The full path and name of the file. It is best to use a variable to hold all this: Dim File. Name As String = Cur. Dir() & ". . 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. 24/12/2021 Name of file. txt 8

Writing to a Text / Serial / Sequential file Write. Line(1, …) n Puts

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

Reading from a Text / Serial / Sequential file VB uses an imaginary pointer

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 1 st line. 24/12/2021 10

Reading from a Text / Serial / Sequential file Input(1, …) n n Reads

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

Reading from a Text / Serial / Sequential file Input or Line. Input should

Reading from a Text / Serial / Sequential file Input or Line. Input 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) EOF = End Of File Input(1, …) Or n … = Line. Input(1) n Rest of loop body statements. n Loop This will loop through the whole file.

Closing a file File. Close(1) Ref no. 24/12/2021 13

Closing a file File. Close(1) Ref no. 24/12/2021 13

Program 7. 1 a Text / Serial / Sequential File Specification: n n Allow

Program 7. 1 a Text / Serial / Sequential File Specification: n n Allow the user to enter people’s names and dates of birth 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). 24/12/2021 14

Program 7. 1 a Text / Serial / Sequential File ‘Declare a variable to

Program 7. 1 a Text / Serial / Sequential File ‘Declare a variable to hold the name and path of the file. Dim File. Name As String = Cur. Dir() & "Names. And. Ages. txt“ Dim Name As String = “” Dim DOB As Date Dim Enter. Another. Student As Boolean Dim Request As String Dim Again As Boolean Dim Age As Integer Dim Number. Of. Adults As Integer 24/12/2021 15

Program 7. 1 a Text / Serial / Sequential File Do n n n

Program 7. 1 a Text / Serial / Sequential File Do n n n Console. Write. Line(“Do you wish to ‘Add’, ‘Display’, ‘Reset’ or ‘Exit’? ”) Request = Console. Read. Line If Request = “Add” Then Console. Write. Line(“Please enter the person’s name. ”) Name = Console. Read. Line Console. Write. Line(“Please the person’s DOB. ”) DOB = Console. Read. Line ‘Open the file to add new data. File. Open(1, File. Name, Open. Mode. Append) Write. Line(1, Name, DOB) ‘Add the name and age to the file. File. Close(1) ‘Close the file. 24/12/2021 16

Program 7. 1 a Text / Serial / Sequential File Else. If Request =

Program 7. 1 a Text / Serial / Sequential File Else. If Request = “Display” Then n Number. Of. Adults = 0 File. Open(1, File. Name, Open. Mode. 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, DOB) ‘Read the DOB. Age = Date. Diff(Date. Interval. Year, DOB, Today) ‘Calculate the Age. Console. Write. Line(Name & “ ” & Age) ‘Displays name and age to the on the console. If Age >= 18 Then ‘Count the number of adults. n Number. Of. Adults +=1 End If n n Loop File. Close(1) ‘Close the file. ‘Output the number older than 18 at the end of the list. Console. Write. Line("Number of adults: " & Number. Of. Adults) 24/12/2021 17

Program 7. 1 Text / Serial / Sequential File n Else. If Request =

Program 7. 1 Text / Serial / Sequential File n Else. If Request = “Reset” Then ‘Clear the file. File. Open(1, File. Name, Open. Mode. Output) File. Close(1) ‘Close the file. n End If Loop Until Request = “Exit” 24/12/2021 18

Program 7. 1 a Text / Serial / Sequential File Run the program and

Program 7. 1 a Text / Serial / Sequential File Run the program and test it. Do you know why we have not used Print for writing to or Line. Input to read from the file? n Try using Print for writing to and Line. Input to read from the file to see what happens if you are not sure. Why does the program crash? Please explain the reasons why we should not use Print and Line. Input in this situation, in your comments. n Just to make sure you change back when you are done. 24/12/2021 19

How to find the file manually. As explained, if Cur. Dir() is used then

How to find the file manually. As explained, if Cur. Dir() is used then files are stored relative to the program; this is always in the Program’s folder: n There will be another folder inside the initial Program’s folder e. g. Text File – Text File. bin n Debug The file should be visible here and you can simply double click to open it in your default text file editor e. g. Notepad.

Commenting on Files In presentations 7. 1 – 7. 2 I will only ask

Commenting on Files In presentations 7. 1 – 7. 2 I will only ask for comments to files. Your comments MUST explain: n What is the file for? n 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? n 24/12/2021 When in the procedure code? 21

Extension 7. 1 b: “Reading Habits” Anna wants to find out about her fellow

Extension 7. 1 b: “Reading Habits” 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: n n 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: n n First. Name Date. Of. Birth Book. Type Reads. Novels 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 Book. Type (printed, audio-book or e-book). 24/12/2021 22

Extension 7. 1 c: “AS Computer Science Students” Write a program to store all

Extension 7. 1 c: “AS Computer Science Students” 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 Print. Line or Write. Line. On running the program the user should be offered the choice to enter more students, see the current list or clear the file. After entering a student the program should constantly ask the user if they wish to continue and, if not, automatically display the current list. 24/12/2021 23

Plenary Explain what a file does and why a file is useful. n n

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

Plenary What are the restrictions of a text file? 24/12/2021 25

Plenary What are the restrictions of a text file? 24/12/2021 25

Text Files Have no natural built in structure to store records since data is

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: n Text files are Sequential files. 24/12/2021 26

Sequential Access Restrictions When you read data you have to read all the data

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 n add on the end i. e. append n clear all data and start again. or Not really possible to change the data in a text file. 24/12/2021 27

Plenary What is the general form of code used to work with files? n

Plenary What is the general form of code used to work with files? n n Dim File. Name As String = My. Application. Info. Directory. Path _ & ". . txt" File. Open(1, File. Name, Open. Mode. Input/Append/Output) Do While Not EOF(1) Write. Line(1, …) / Print. Line(1, …) … = Line. Input(1) / Input(1, …) Rest of Loop Body n n Loop File. Close(1) 24/12/2021 28