Visual C# Guide
Binary Files

Binary files are an alternative to text files. They are used to store records. Data types other than strings and characters are displayed according to the the rules of the format and cannot be displayed in a meaningful way in a text editor. In order to read from a binary file, you need to know its structure.

The example program allows the user to input data to create a new text file (you should change the path to something that your user permissions allow). When the data have been entered, the contents of the file are read and displayed at the console. The reading and writing processes have been created as procedures to make the program easier to read.

static TPerson person = new TPerson();

public struct TPerson
{
   public string initial;
   public string surname;
   public int age;
}

static void WriteData()
{
   Console.Clear();
   Console.WriteLine("Data Entry");
   string path = @"C:\test.bin";
   FileStream fs = new FileStream(path, FileMode.CreateNew);
   BinaryWriter bw = new BinaryWriter(fs);
   string continueAdding;
   do
   {
      Console.Write("Enter the initial: ");
      person.initial = Console.ReadLine();
      Console.Write("Enter the surname: ");
      person.surname = Console.ReadLine();
      Console.Write("Enter the age: ");
      person.age = System.Convert.ToInt32(Console.ReadLine());
      Console.WriteLine();
      bw.Write(person.initial);
      bw.Write(person.surname);
      bw.Write(person.age);
      Console.WriteLine("Add another (y/n)? ");
      continueAdding = Console.ReadLine();
   }
   while (continueAdding == "y");
   bw.Close();
   fs.Close();
}

static void ReadData()
{
   Console.Clear();
   Console.WriteLine("Reading Data");
   Console.WriteLine();
   string path = @"C:\test.bin";
   FileStream fs = new FileStream(path, FileMode.Open);
   BinaryReader br = new BinaryReader(fs);
   do
   {
      person.initial = br.ReadString();
      person.surname = br.ReadString();
      person.age = br.ReadInt32();
      Console.WriteLine("{0} {1}, {2}", person.initial, person.surname, person.age);
      Console.WriteLine();
   }
   while (fs.Position < fs.Length);
   br.Close();
   fs.Close();
}

static void Main(string[] args)
{
   WriteData();
   ReadData();
   Console.ReadLine();
}

Look carefully at the lines of code that open the filestreams. The FileMode enumeration specifies what you want to do with the file. There are other modes,

FileModeDescription
FileMode.AppendOpens the file if it exists and moves to the end of the file. You cannot read the file using this mode.
FileMode.CreateSpecifies that a new file should be created. If the file exists, it will be overwritten.
FileMode.CreateNewSpecifies that a new file should be created. An exception occurs if the file exists.
FileMode.OpenOpens the file if it exists, throws an exception if not.
FileMode.OpenOrCreateOpens the file if it exists or creates a new one if it does not.
FileMode.TruncateOpens an existing file and wipes its contents.

Having A Go

  1. Write a program that reads the CSV file provided on the previous page and creates a binary file with the data. Display all of the records from the binary file that you create.