Advertisement
svetlozar_kirkov

Binary Read-Write (Exercise)

Oct 9th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace ConsoleTests
  5. {
  6.     class ConsoleTests
  7.     {
  8.         static void Main()
  9.         {
  10.         W();
  11.             R();
  12.             Console.WriteLine();
  13.         }
  14.  
  15.         static void W()
  16.         {
  17.             int[] a = new int[]
  18.             {
  19.                 1,
  20.                 4,  
  21.                 6,
  22.                 7,
  23.                    11,
  24.                    55,
  25.                   777,
  26.                    23,
  27.                   266,
  28.                    44,
  29.                    82,
  30.                    93
  31.             };
  32.        
  33.             using (BinaryWriter b = new BinaryWriter(File.Open("file.bin", FileMode.Create)))
  34.             {
  35.                 foreach (int i in a)
  36.                 {
  37.                b.Write(i);
  38.                 }
  39.             }
  40.         }
  41.  
  42.         static void R()
  43.         {
  44.             using (BinaryReader b = new BinaryReader(File.Open("file.bin", FileMode.Open)))
  45.             {
  46.                 int pos = 0;
  47.                 int length = (int)b.BaseStream.Length;
  48.                 while (pos < length)
  49.                 {
  50.                     int v = b.ReadInt32();
  51.                     Console.Write(v + " ");
  52.  
  53.                     pos += sizeof(int);
  54.                 }
  55.             }
  56.         }
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement