Random Access Files Dr. John Abraham.

Slides:



Advertisements
Similar presentations
Midterm 26 March 2015 (4:30-5:30 pm) – Rm5620 Closed book exam MC Questions only x25 Up to L(7) Methods Scope: video lectures (+Lab), forum discussions,
Advertisements

Practical Programming COMP153-08S Lecture: Repetition Continued.
COPYRIGHT 2003: Dr. David Scanlan, CSUS OBJECTIVES: Explain the need for arrays. Coding an array. Basic algorithms: Largest, Smallest, Sum, Standard Deviation,
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
File dialog boxes Function args and overloading. Examples in this show List directory contents Open a file for viewing Read/Write to a file Add file I/O.
Arrays. Declaring a Array With subscript: –Dim numbers(2) as Integer –Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex)
More on lists, exceptions, loops and validation. You can use the exception to indicate the error that occurred Private Sub btnCheck_Click(ByVal sender.
A short ppt Importing images Changing fonts. Getting images.
Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales.
Chapter 13 Lists. List  List  A variable-length, linear collection of homogeneous components  Example  StudentRec Students[100];  A list of 100 students.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Structural Pattern: Decorator There are times when the use of subclasses to modify the behavior of individual objects is problematic. C h a p t e r 4.
COMPUTER PROGRAMMING I Objective 7.03 Apply Built-in Math Class Functions.
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
TRAVERSING AN ARRAY My friend, the FOR loop Analyze an array in other ways than max,min,sort… 11.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
CS0004: Introduction to Programming Project 1 – Lessons Learned.
10-1 Chapter 10 Security, Menus, Files, and Graphics.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Delivery and other DO Examples Please use speaker notes for additional information!
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Problem Solving with Decisions
PRACTICE SAC 1 Some solutions. 'inputs 'sale price 'postcode 'coupon codes 'processing 'calculate commissions (commission based on cost) 'calculate postage.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
1 Microsoft® Visual Basic®.NET Language # 2. 2 Flow-Control Statements If … End If Select Case … End Select For… Next Do … Loop Exit.
Practical Programming COMP153-08S Lecture: Repetition.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
Subs and Functions Ch 6. Introduction VB.NET Procedures Sub Procedures Function Procedures Outline.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
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.
Computer Science Up Down Controls, Decisions and Random Numbers.
Directory and File. Access Files in a Directory Name space: System.IO The Directory and File classes contain only shared methods that set or return information.
Sub Procedures And Functions
Use TryParse to Validate User Input
Objective 7.04 Apply Built-in String Functions (3%)
Objective 7.03 Apply Built-in Math Class Functions
Data Searching and Sorting algorithms
Searching Given a collection and an element (key) to find… Output
Single Dimensional Arrays
Use TryParse to Validate User Input
Introduction to VB programming
Files and Streams.
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
TreeView Control.
للمزيد زورونا على موقعنا الإلكتروني:
CS 3870/CS 5870 Web User Controls Events (II).
Boolean Expressions and If statements
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
String Variable, Methods and Properties
searching Concept: Linear search Binary search
CSCI 3327 Visual Basic Review: Final Exam
Searching and Sorting 1-D Arrays
String Variable, Methods and Properties
Given value and sorted array, find index.
Random Access Files / Direct Access Files
String Variable, Methods and Properties
Coding ADO.NET Objects: Connection, Command, DataReader
String Variable, Methods and Properties
Part 2 Saving the Dictionary
RANDOM NUMBERS SET # 1:
Presentation transcript:

Random Access Files Dr. John Abraham

Advantages of Random Access Access one record without disturbing others Modify that record Create Index if needed to access it many ways

Structure Module modulePersonRec Structure personRecord <VBFixedString(7)> Public RecordNumber As String <VBFixedString(9)> Public SS As String <VBFixedString(40)> Public Name As String <VBFixedString(10)> Public Tele As String <VBFixedString(2)> Public LineBreak As String End Structure

Using Structure Public DATAFILE As String = "mydb.mdf" Public Person As personRecord Public Position As Long Public LastRecord As Long Public FileLength As Long Public indexSS(100) As String Public indexKey(100) As Long End Module

Opening a Random file Private Sub btnOpen_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click Position = 1 ' Calculate the record length. RecLength = Len(Person) FileOpen(1, DATAFILE, OpenMode.Random, , , RecLength) FileLength = LOF(1) LastRecord = FileLength / RecLength lblConnected.Text = "Connected... Total " & LastRecord & " Records Found. " lblRecNo.Text = LastRecord + 1 End Sub

Writing to a Random File Person.SS = txtSSN.Text Person.Name = txtName.Text Person.Tele = txtTele.Text Person.LineBreak = Chr(13) & Chr(10) LastRecord = LOF(1) / RecLength Person.RecordNumber = LastRecord + 1 'If LastRecord > 0 Then findLink(link) FilePut(1, Person, LastRecord + 1)

Reading from a Random File FileGet(1, Person, Val(txtKey.Text)) txtSSN.Text = Person.SS txtName.Text = Person.Name txtTele.Text = Person.Tele

Reading one after another (scanning) Private Sub tabScan_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabScan.Click Position = 1 lblPosition.Text = Position FileGet(1, Person, Position) lblSS.Text = Person.SS lblName.Text = Person.Name lblTele.Text = Person.Tele End Sub

Displaying a record Dim tmps As String With Person lblPosition.Text = Person.RecordNumber tmps = Trim(Person.SS) lblSS.Text = tmps.Substring(0, 3) & "-" & tmps.Substring(3, 2) & "-" & tmps.Substring(5) lblName.Text = Trim(Person.Name) tmps = Trim(Person.Tele) lblTele.Text = tmps.Substring(0, 3) & "-" & tmps.Substring(3, 3) & "-" & tmps.Substring(6) End With

Linear Search For i = 1 To LOF(1) / RecLength FileGet(1, Person, i) If Person.Name.IndexOf(txtNameSearch.Text) > -1 Then lblNameS.Text = Person.Name lblTeleS.Text = Person.Tele lblSSnS.Text = Person.SS lblRecS.Text = i Exit For Else lblNameS.Text = "Not Found" lblTeleS.Text = "" lblSSnS.Text = "" lblRecS.Text = 0 End If

Creating and Index on SS LastRecord = LOF(1) / RecLength For i = 1 To LastRecord FileGet(1, Person, i) indexSS(i) = Person.SS indexKey(i) = i Next

Sorting Index i = LastRecord Do sorted = True For j = 1 To i - 1 If indexSS(j) > indexSS(j + 1) Then sorted = False temp1 = indexSS(j) : temp2 = indexKey(j) indexSS(j) = indexSS(j + 1) : indexKey(j) = indexKey(j + 1) indexSS(j + 1) = temp1 : indexKey(j + 1) = temp2 End If Next j i = i - 1 Loop While Not sorted ' (sorted = False) MessageBox.Show("Index Sorted.", "Sort")

Binary Search Lookfor = Trim(txtSSnS.Text) nfound = False first = 1 : last = LastRecord While (Not (nfound) And first <= last) middle = (first + last) \ 2 MessageBox.Show(Lookfor & " " & indexSS(middle), "Binary search demonstration ") If Lookfor = Trim(indexSS(middle)) Then nfound = True FileGet(1, Person, indexKey(middle)) lblNameS.Text = Person.Name lblTeleS.Text = Person.Tele lblSSnS.Text = Person.SS lblRecS.Text = indexKey(middle) ElseIf Lookfor < indexSS(middle) Then last = middle - 1 Else first = middle + 1 End If End While If Not (nfound) Then lblNameS.Text = "Not Found!"