Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text / Serial / Sequential Files

Similar presentations


Presentation on theme: "Text / Serial / Sequential Files"— Presentation transcript:

1 Text / Serial / Sequential Files
12/07/2019

2 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

3 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

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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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.

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

14 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

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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

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

25 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

26 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

27 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

28 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


Download ppt "Text / Serial / Sequential Files"

Similar presentations


Ads by Google