1 / 10

File Processing

File Processing . Creating & Reading Text Files. Storing Data in Text Files: With VB 2008. File>New Project> WindowsFormApplication (provide a project name) Highlight project name in Solution Explorer. Add>New Item Select Text File icon> provide name Type in data values, one per line

faith
Télécharger la présentation

File Processing

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. File Processing Creating & Reading Text Files

  2. Storing Data in Text Files: With VB 2008 • File>New Project>WindowsFormApplication • (provide a project name) • Highlight project name in Solution Explorer. • Add>New Item • Select Text File icon> provide name • Type in data values, one per line • Save All

  3. Data for text file: Payroll.txt Sandy Kinnery 9.35 35 Judy Mollica 10.75 33

  4. Problem: Calculate Payroll

  5. Syntax to Read in Data Sequentially from File Dim readerVar As IO.StreamReader readerVar = IO.File.OpenText(filespec) Varname = readerVar.ReadLine filespecmust include the full pathname unless the file is in ProjectFolderName/bin/Debug

  6. Code In Button Click-Event Dim readerVar As IO.StreamReader Dim strName As String Dim decHourlyWage As Decimal Dim decHoursWorked As Decimal Dim decSalary As Decimal readerVar = IO.File.OpenText("Payroll.txt")

  7. Read in Data from Text File strName = readerVar.ReadLine decHourlyWage = CDec(readerVar.ReadLine) decHoursWorked = CDec(readerVar.ReadLine) decSalary = decHourlyWage * decHoursWorked

  8. Display Results in a ListBox lstResults.Items.Add(strName & " " & _ FormatCurrency(decSalary)) readerVar.Close()

  9. Peek Method of StreamReader Class: Returns -1 when EOF • readerVar = IO.File.OpenText("Payroll.txt") Do While readerVar.Peek <> -1 strName = readerVar.ReadLine decHourlyWage = CDec(readerVar.ReadLine) decHoursWorked = CDec(readerVar.ReadLine) decSalary = decHourlyWage * decHoursWorked lstResults.Items.Add(strName & " " & _ FormatCurrency(decSalary)) Loop readerVar.Close()

  10. Text Readings • ListBox – pp. 294-299 • Text Files – pp. 658-671 • Not exactly what we’ve done today

More Related