Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6726609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:56:50+00:00 2026-05-26T09:56:50+00:00

I am doing homework and I got to the part where I need to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:56:50+00:00Added an answer on May 26, 2026 at 9:56 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a c homework problem that is doing my head in and will
Im doing a homework problem for computer architecture and im stumped on using a
I'm doing my homework on java I/O data, the problem is i'm not allowed
I'm new to c++ and got a problem when doing homework (sudoku). Instruction said:
Im doing a homework problem where I need to run the bellman-ford algorithm starting
No problems here, just need explanation how does that work. I was doing homework
I am doing homework in Matlab, calculating numeric integration using different methods like simpson,
I am doing homework but I have problem with non-RestFul routes. My spec is:
I am doing a homework assignment for my summer OO class and we need
I am doing a homework assignment for my summer OO class and we need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.