Reading a sequential-access file : Binary Read Write « File Stream « C# / C Sharp






Reading a sequential-access file

Reading a sequential-access file
  
   using System;
   using System.Data;
   using System.IO;
   using System.Runtime.Serialization.Formatters.Binary;
   using System.Runtime.Serialization;

   public class CreateFile {
      static void Main() {
         BinaryFormatter formatter = new BinaryFormatter();
         FileStream output = new FileStream( "test.dat",FileMode.OpenOrCreate, FileAccess.Write );
         Record record = new Record();
         record.Account = 1234;
         record.FirstName = "FirstName";
         record.LastName = "LastName";
         record.Balance = 1234.345;

         formatter.Serialize( output, record );
         output.Close();
      
      
         BinaryFormatter reader = new BinaryFormatter();
         FileStream input = new FileStream( "test.dat", FileMode.Open, FileAccess.Read );
         Record record1 =( Record )reader.Deserialize( input );
         
         Console.WriteLine(record1.FirstName);
      }
   }
   [Serializable]
   public class Record{
      public int Account;
      public String FirstName;
      public String LastName;
      public double Balance;
   }


           
         
    
  








Related examples in the same category

1.Binary Writer Reader
2.new BinaryReader(stream)
3.StreamWriter and BinaryWriter
4.new BinaryWriter(stream) and Write method
5.Creating a sequential-access file
6.BinaryWriter and BinaryReader classes for the writing and reading of primitive typesBinaryWriter and BinaryReader classes for the writing and reading of primitive types
7.Working with the BinaryWriter Class
8.Working with the BinaryReader ClassWorking with the BinaryReader Class
9.File pointer move and read binary file
10.Write and then read back binary dataWrite and then read back binary data
11.Use BinaryReader and BinaryWriter to implement a simple inventory programUse BinaryReader and BinaryWriter to implement 
   a simple inventory program
12.Read the data from a file and desiralize it
13.Read and Write a Binary File
14.Read Struct out of a file with BinaryReader
15.extends BinaryReader
16.Write a value at a given position. Used to write a size of data in an earlier located header.