How to use BinaryReader in loop so I can display chunks of information in correct format?

StackOverflow https://stackoverflow.com/questions/7830908

  •  27-10-2019
  •  | 
  •  

Question

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:

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

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.

I appreciate any tips how this could be done.

Thank You!

Here are my code files:

Program.cs

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();
        }
    }
}

Publication.cs

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);
        }
    }
}

Book.cs

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);
        }
    }
}

Thank you for any help.

Regards.

HelpNeeder.

-- EDIT --

Works! Thank you!

    while (read.PeekChar() != -1)
    {
        book.readDataFrom(read);
        book.display();
    }

Also, I had to make sure that new file is being created each time I ran the program!

Plus I have to make sure that I close my FileStreams because I keep crashing program.

Was it helpful?

Solution

You forget to perform a loop. book.display only shows one book. You also forget to read in data for the book.

You can check if there is more data in the file by doing a peek. If peek returns -1 you know there is no more data.

Example:

FileStream readStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader read = new BinaryReader(readStream);

while (read.PeekChar() != -1)
{
    book.readDataFrom(read);
    book.display();
}

OTHER TIPS

Use binaryReader.PeekChar() method.

while(binaryReader.PeekChar()!=-1)
{
//
}

EDIT:

FileStream readStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
BinaryReader read = new BinaryReader(readStream);
Book book = new Book();
 while(read.PeekChar()!=-1)
    {
      book.readDataFrom(read);
      book.display();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top