1 / 23

ITDEV 130 Chapter 10 Structures and Sequential Access Files

ITDEV 130 Chapter 10 Structures and Sequential Access Files. Lesson A: Create a Structure. Declare a structure variable. Pass a structure variable to a procedure. Create and manipulate a one-dimensional array of structures. Syntax Structure structureName

mab
Télécharger la présentation

ITDEV 130 Chapter 10 Structures and Sequential Access Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITDEV 130 Chapter 10 Structures and Sequential Access Files

  2. Lesson A: • Create a Structure. • Declare a structure variable. • Pass a structure variable to a procedure. • Create and manipulate a one-dimensional array of structures.

  3. Syntax Structure structureName Public memberVariableNameAsdataType [Public memberVariableNameAs dataType] End Structure

  4. Example Structure Account Public strID As String Public dblBeginingBal As Double Public dblDeposits As Double Public dblWithdrawals As Double End Structure

  5. Declaring a Structure Variable {Dim | Private} structureVariableNameAsstructureName Dim Checking As Account

  6. Using a member variable Checking.AcBeginingBal = 2000.00 lblAccountID.Text = Checking.AcID

  7. Passing a Structure to a Procedure Previously: Public Sub btnCalc(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim strlID As String Dim dblBeginingBal As Double Dim dblDeposits As Double Dim dblWithdrawals As Double AcBeginingBal = NewBal(dblBeginingBal,dblDeposits,dblWithdrawal) End Sub Public Function NewBal(ByValdblBegBal As Double,ByValdblDep As Double,ByValdblWith As Double) As Double return dblBegBal + dblDep – dblWith End Function

  8. With Structure: Structure Account Public strID As String Public dblBeginingBal As Double Public dblDeposits As Double Public dblWithdrawals As Double End Structure Public Sub btnCalc(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim Checking As Account Checking.strID = “Checking” Checking.dblBeginingBal = NewBal(Checking) End Sub • Public Function NewBal(ByVal HA As Account) As Double • Return HA .dblBegBal + HA.dblDep– HA.dblWith • End Function

  9. Array of Structure Variables Structure Account Public strID As String Public dblBeginingBal As Double Public dblDeposits As Double Public dblWithdrawals As Double End Structure Public Sub btnCalc(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim MyAccounts(4) As Account MyAccount(1).strID = “Checking” MyAccounts(1).dblBeginingBal = NewBal(MyAccounts(1)) End Sub Public Function NewBal(ByVal HA As Account) As Double Return HA .dblBegBal + HA .dblDep – HA .dblWith End Function

  10. Lesson B: • Types of files. • Open and close a sequential file. • Write data to a sequential file. • Read data from a sequential file. • Determine whether a sequential access file exists. • Test for the end of a sequential file.

  11. Types of Files

  12. Sequential Access Files– Early file type. 256 characters per record. Fields are delimited. Record terminated with a LF/CR. Migration from card data. Start at the beginning and move record by record through the end. Can be opened for Append Either Read or Write, not both at same time.

  13. Direct or Random Access Files- • Records may be Read or Written. • Read/Write pointer may be moved to any record (hence direct or random). • Fixed record/field sizes.

  14. Binary Files- Really Direct Files typically with some type of media data (pics, video, sound).

  15. Database Files- Data is structured based on either a hierarchical, network, or relational schema. Database provides tools to: Handel SQL queries. Create & manage the Database. Provide Ad-hoc reporting.

  16. Writing data to an output file using sequential access: In Visual Basic we use something called a SreamWriter object to write data to an output file. Declaring a StreamWriter variable: {Dim|Private} streamWriterVariable name AsIO.StreamWriter Dim outFile As IO.StreamWriter

  17. Creating a StreamWriter object by opening a sequential access file: IO.File.Method(filename) outFile = IO.File.CreateText(“Student.dat”) opens the Student.dat file for output; creates a SteramWriter object and assignsd it to the outfile variable. outFile = IO.File.AppendText(“MyFile.dat”) opens the report.dat file for a\append; creates a StreamWriter object and assigns it to the outFile variable. Note: When the directory is not qualified in the file name, the default location for the file will be the same directory that the .exe file ran from.

  18. Writing data. outFile.Write(“Hello”) – no cr/lf. outFile.WriteLine(“Hello”) – adds cr/lf to end of data.

  19. Reading Data From A Sequential Access File: Syntax {Dim|Private} streamReaderVariableNameAs IO.StreamReader Dim inFile As IO.StreamReader

  20. Creating a StreamReader object by opening a sequential access file Syntax IO.File.OpenText(filename) inFile = IO.File.OpenText(“Student.dat”)

  21. Determining whether a file exists IO.File.Exists(filename) If IO.File.Exists(“Student.dat”) Then ….

  22. Reading data from a sequential access file Syntax straemReaderVariableName.ReadLine strHldData = inFile.ReadLine

  23. Reading data from a file until the end of file is reached: • Do Until inFile.Peek = -1 • strHldData = inFile.ReadLine • do something with the data • Loop

More Related