Search code examples
c#methodsbinaryreaderbinarywriter

How to use BinaryReader and correctly input data into file?


I am working on my homework assignment and I am completely stuck! What I am trying to do is to use already defined input and save it to the file by using saveDataTo() method and read the input by using readDataFrom() method.

I am stuck on the first part. I am not sure if I have to initialize the data in Program.cs file first?

I don't know and I am stuck. Here is code and hope for some tips how I can accomplish this.

-- EDIT --

I can add instructions for purpose of both saveDataTo() and readDataFrom() methods:

The saveDataTo( ) method takes a parameter of BinaryWriter. The method writes the values of all 5 properties of an book object to a file stream associated with the writer (the association is done in the Main( ) method of Program class). There is no need to open and close the file stream and binary writer inside this method.

The readDataFrom( ) method takes a parameter of BinaryReader. The method reads the values of all five properties of the Book object from a file stream associated with the reader (the association is done in the Main( ) method of Program class). There is no need to open and close the file stream and binary reader inside this method.

So that gives me a clue that I should use and assign the properties to be saved in the file there?

-- EDIT --

Updated the code there. I do have a problem with content that is being saved into the file. I am not being showed the price. Why is that?

ff.APublisherNameTitle  FirstNameLastName

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;

            /*
            do
            {
                Console.Write("Enter Book Title: ");
                publication.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: ");
                publication.PublisherName = Console.ReadLine();
                Console.Write("Enter Book Price: $");
                publication.Price = float.Parse(Console.ReadLine());
                Console.Write("Would like to enter another book? [Y or N] ");
                ask = char.Parse(Console.ReadLine().ToUpper());
            }
            while (ask == char.Parse("Y"));
            */

            Book book = new Book();

            book.Price = 10.9F;
            book.Title = "Title";
            book.PublisherName = "PublisherName";
            book.AuthorFirstName = "FirstName";
            book.AuthorLastName = "LastName";

            FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
            BinaryWriter write = new BinaryWriter(fileStream);
            book.saveDataTo(write);
            write.Close();

            fileStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
            BinaryReader read = new BinaryReader(fileStream);
            book.readDataFrom(read);
            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()
        {
        }

        public string getAuthorName()
        {
            return authorFirstName + " " + authorLastName;
        }

        public void readDataFrom(BinaryReader r)
        {
            Price = r.ReadInt32();
            PublisherName = r.ReadString();
            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);
        }
    }
}

Regards.

HelpNeeder.


Solution

  • You assign your parameters to 2 different objects, see:

    Publication publication = new Publication(); 
    Book book = new Book(); 
    

    Both are individual instances residing in memory.

    You either have to refer the publication to the book like:

    Book book = new Book(); 
    Publication publication = (Publication)book;
    

    or just assign the values currently assigned to the publication directly to the book so:

    publication.PublisherName = "PublisherName"; 
    

    becomes

    book.PublisherName = "PublisherName"; 
    

    Apart from that, you're working in C#, not Java. By convention its normal to start your methods with a Capital (Pascal Case)

    EDIT

    Your now shown the price when reaidng since you write it as a floating field (or double, cant see the definition) and read it as an integer.

    Change from r.ReadInt32(); to r.ReadDouble(); or r.ReadSingle()