Visual C# 2005 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,

string textToWrite = 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.

static TPerson[] people = new TPerson[21];
public struct TPerson
{
   public string initial;
   public string surname;
   public string address1;
   public string address2;
   public string address3;
   public string postcode;
   public int age;
}

static void ReadRecords()
{
   string path = @"C:\names.csv";
   StreamReader sr = new StreamReader(path);
   sr.ReadLine();
   string temp;
   string[] tempLine;
   int counter = 0;
   while (!sr.EndOfStream)
   {
      temp = sr.ReadLine();
      tempLine = temp.Split(new Char [] { ',' });
      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 = System.Convert.ToInt32(tempLine[6]);
      counter++;
   }
   sr.Close();
}

static int GetMenuChoice()
{
   Console.Clear();
   Console.WriteLine("Address Book Menu");
   Console.WriteLine();
   Console.WriteLine("1. Search For A Record");
   Console.WriteLine("2. Quit");
   return System.Convert.ToInt32(Console.ReadLine());
}

static void ShowRecord(int recnum)
{
   Console.WriteLine("{0} {1}", people[recnum].initial, people[recnum].surname);
   Console.WriteLine(people[recnum].address1);
   Console.WriteLine(people[recnum].address2);
   Console.WriteLine(people[recnum].address3);
   Console.WriteLine(people[recnum].postcode);
   Console.WriteLine("Age: {0}", people[recnum].age);
   Console.WriteLine();
   Console.WriteLine("Press enter to continue");
   Console.ReadLine();
}

static void Main(string[] args)
{
   ReadRecords();
   Console.WriteLine("Records Read");
   int menuchoice = GetMenuChoice();
   int itemtosearch;
   while (menuchoice==1)
   {
      Console.Write("Enter the number of the record you want to find ");
      itemtosearch = System.Convert.ToInt32(Console.ReadLine());
      ShowRecord(itemtosearch);
      menuchoice = GetMenuChoice();
   }
}

Having A Go

  1. 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.
  2. 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.