title: How to use BinaryReader in loop so I can display chunks of information in correct format?
body: <p>I am doing homework and I got to the part where I need to display my data from data file. The problem is that I can display individual data by using BinaryReader() but I can not create a correct loop which would display all data in format specified below:</p>
  
  <pre><code>Enter Book Title: Title 1
  Enter Author's First Name: First 1
  Enter Author's Last Name: Last 1
  Enter Publisher's Name: Publisher 1
  Enter Book Price: $1.1
  Would like to enter another book? [Y or N] y
  Enter Book Title: Title 2
  Enter Author's First Name: First 2
  Enter Author's Last Name: Last 2
  Enter Publisher's Name: Publisher 2
  Enter Book Price: $2.2
  Would like to enter another book? [Y or N] n
  
  Title 1
  Publisher 1
  1.1
  First 1 Last 1
  
  Title 2
  Publisher 2
  2.2
  First 2 Last 2
  </code></pre>
  
  <p>Instead, I am showing only the last entry. See the problem? I do not know how to display all data from data folder by using a loop.</p>
  
  <p>I appreciate any tips how this could be done.</p>
  
  <p>Thank You!</p>
  
  <p>Here are my code files:</p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  
  namespace Lab_7
  {
      class Program
      {
          private const string FILE_NAME = "lab07.dat";
  
          static void Main(string[] args)
          {
              char ask;
  
              if (!File.Exists(FILE_NAME))
              {
                  FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
              }
  
              Book book = new Book();
  
              FileStream writeStream = new FileStream(FILE_NAME, FileMode.Append);
              BinaryWriter write = new BinaryWriter(writeStream);
  
              do
              {
                  Console.Write("Enter Book Title: ");
                  book.Title = Console.ReadLine();
                  Console.Write("Enter Author's First Name: ");
                  book.AuthorFirstName = Console.ReadLine();
                  Console.Write("Enter Author's Last Name: ");
                  book.AuthorLastName = Console.ReadLine();
                  Console.Write("Enter Publisher's Name: ");
                  book.PublisherName = Console.ReadLine();
                  Console.Write("Enter Book Price: $");
                  book.Price = float.Parse(Console.ReadLine());
                  Console.Write("Would like to enter another book? [Y or N] ");
                  ask = char.Parse(Console.ReadLine().ToUpper());
  
                  book.saveDataTo(write);
              }
              while (ask == char.Parse("Y"));
  
              write.Close();
  
              FileStream readStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
              BinaryReader read = new BinaryReader(readStream);
  
              book.display();
  
              read.Close();
          }
      }
  }
  </code></pre>
  
  <p><strong>Publication.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  
  namespace Lab_7
  {
      class Publication
      {
          private float price;
          private string publisherName, title;
  
          public float Price
          {
              get
              {
                  return price;
              }
              set
              {
                  price = value;
              }
          }
  
          public string PublisherName
          {
              get
              {
                  return publisherName;
              }
              set
              {
                  publisherName = value;
              }
          }
  
          public string Title
          {
              get
              {
                  return title;
              }
              set
              {
                  title = value;
              }
          }
  
          public void display()
          {
              Console.WriteLine("{0}\n{1}\n{2}", title, publisherName, price);
          }
      }
  }
  </code></pre>
  
  <p><strong>Book.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.IO;
  
  namespace Lab_7
  {
      class Book : Publication
      {
          private string authorFirstName, authorLastName;
  
          public string AuthorFirstName
          {
              get
              {
                  return authorFirstName;
              }
              set
              {
                  authorFirstName = value;
              }
          }
  
          public string AuthorLastName
          {
              get
              {
                  return authorLastName;
              }
              set
              {
                  authorLastName = value;
              }
          }
  
          public new void display()
          {
              base.display();
              Console.WriteLine("{0}", getAuthorName());
          }
  
          public string getAuthorName()
          {
              return authorFirstName + " " + authorLastName;
          }
  
          public void readDataFrom(BinaryReader r)
          {
  
              base.Price = r.ReadSingle();
              base.PublisherName = r.ReadString();
              base.Title = r.ReadString();
              authorFirstName = r.ReadString();
              authorLastName = r.ReadString();
          }
  
          public void saveDataTo(BinaryWriter w)
          {
              w.Write(base.Price);
              w.Write(base.PublisherName);
              w.Write(base.Title);
              w.Write(AuthorFirstName);
              w.Write(AuthorLastName);
          }
      }
  }
  </code></pre>
  
  <p>Thank you for any help.</p>
  
  <p>Regards.</p>
  
  <p>HelpNeeder.</p>
  
  <p>-- EDIT --</p>
  
  <p>Works! Thank you!</p>
  
  <pre><code>    while (read.PeekChar() != -1)
      {
          book.readDataFrom(read);
          book.display();
      }
  </code></pre>
  
  <p>Also, I had to make sure that new file is being created each time I ran the program!</p>
  
  <p>Plus I have to make sure that I close my FileStreams because I keep crashing program.</p>