Visual Basic 2010 (Console) Guide
CSV Files

CSV stands for comma-separated values or comma-separated variable. The file, names.csv is an example of a CSV file. Microsoft Excel exports to this format and, if you have it installed on your machine, the chances are that a file association has been made to ensure that these files open with Excel. Make sure you use Notepad so that you see the actual format.

CSV is an easy format to read and write and, as a result, is quite popular for moving small datasets around.

In this file, the first line is a list of the field names. Each item is separated from the next by a comma. The line break indicates the end of the record.

You can write CSV files in the same way you did plain old text files. You just have to make sure you write in the commas. The easiest way to do this is to prepare the line as a string before writing it to the file. For example,

Dim textToWrite As String = value1 + ", " + value2
sw.WriteLine(textToWrite)

Reading CSV

It's easy to read CSV files using the method outlined a few pages ago. The only thing to consider is how you deal with the information in your program. One way is to use an array of a structure that you define.

Dim people(8) As Tperson

Structure Tperson
   Public initial As String
   Public surname As String
   Public address1 As String
   Public address2 As String
   Public address3 As String
   Public postcode As String
   Public age As Integer
   Public Sub DisplayRecord()
      Console.WriteLine("{0} {1} ({2})", initial, surname, age)
      Console.WriteLine(address1)
      Console.WriteLine(address2)
      Console.WriteLine(address3)
      Console.WriteLine(postcode)
   End Sub
End Structure

Sub Main()
   ReadRecords()
   For counter = 0 To people.Length - 1
      people(counter).DisplayRecord()
      Console.WriteLine()
   Next
   Console.ReadLine()
End Sub

Sub ReadRecords()
   Dim path As String = "C:\names.csv"
   Dim sr As StreamReader = New StreamReader(path)
   sr.ReadLine()
   Dim counter As Integer = 0
   Dim temp As String
   Dim tempLine() As String
   While (Not sr.EndOfStream)
      temp = sr.ReadLine()
      tempLine = Split(temp, ",")
      people(counter).initial = tempLine(0)
      people(counter).surname = tempLine(1)
      people(counter).address1 = tempLine(2)
      people(counter).address2 = tempLine(3)
      people(counter).address3 = tempLine(4)
      people(counter).postcode = tempLine(5)
      people(counter).age = CInt(tempLine(6))
      counter += 1
   End While
   sr.Close()
End Sub

Notice that there is a procedure inside the Structure. In the main sub, there is a call to this procedure. Given that displaying the record depends entirely on the design of the Structure, it makes sense to put the method for display within that block of code.

Having A Go

  1. Copy and compile the example program. Add code to allow the user to search for people by surname. When a match is found, the address should be displayed.
  2. Locate the code that you wrote for the random number guessing game (the one that says higher and lower until you get the number. Adapt the program so that high scores and the names of high-scoring players are stored in a text file.
  3. Adapt your quiz program so that the user can choose from a range of question sets, each loaded into the program from a text file.