1 / 9

Text and Binary Files

Text and Binary Files. Reading and Writing. Making data persistent. When a program finishes, the data stored in variables is lost. Data can be stored in several ways. Text files csv files Binary files Databases. What you need to know. csv files what it is

lacy
Download Presentation

Text and Binary 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. Text and Binary Files Reading and Writing Comp1 notes

  2. Making data persistent • When a program finishes, the data stored in variables is lost. • Data can be stored in several ways. • Text files • csv files • Binary files • Databases Comp1 notes

  3. What you need to know • csv files • what it is • to read records from a csv file • to write records to a csv file • data files • what it is • to read records from a data file • to write records to a data file Comp1 notes

  4. What is a csv file? • Printable characters • Separator between values • Comma • Tab & others are sometimes used • End of line marker Use a spreadsheet and save as csv to make a csv file quickly Comp1 notes

  5. Writing to a text file • Must include IO library Imports System.IO • Declare a writing channel Dim CFW As StreamWriter • Link the file pathname to the channel Filename = “C:\My files\myCsvFile.txt” CFW = New StreamWriter(Filename) • Write a line of text to the channel CFW.WriteLine(“This is a string of text”) • Close the channel CFW.Close() Comp1 notes

  6. Writing to a csvfile • Create a string, using a comma to separate the fields Textstring = (Name & “,” & Telno & “,” & AvPtSc) • Write the string to the file CFW.WriteLine(Textstring) Comp1 notes

  7. Reading from a text file • Must include IO library Imports System.IO • Declare a writing channel Dim CFR As StreamReader • Link the file pathname to the channel Filename = “C:\My files\myCsvFile.txt” CFR = New streamReader(Filename) • Read a line of text from the channel MyString = CFR.ReadLine() • Checkfor end of file CFR.EndOfStream ‘returns boolean • Close the channel CFR.Close() Comp1 notes

  8. Reading from a csv file • You don’t need to know how to use classes to handle csv files. • You do need to know how to handle string functions such as: • length • position • substring • concatenation Comp1 notes

  9. Your mission • Use your Student Record and Student Group array from last lesson • Add five Students • Create a csv file • Write code to write your students to the file • Open your file in a spreadsheet to check • Add 5 more students to the spreadsheet • Write code to read these back into your program as student records and output the whole group to the console window. Comp1 notes

More Related