1

我正在做作业,我到了需要从数据文件显示我的数据的部分。问题是我可以使用 BinaryReader() 显示单个数据,但我无法创建一个正确的循环来显示以下指定格式的所有数据:

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

相反,我只显示最后一个条目。看到问题了吗?我不知道如何使用循环显示数据文件夹中的所有数据。

我很感激任何提示如何做到这一点。

谢谢你!

这是我的代码文件:

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

出版物.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);
        }
    }
}

图书.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);
        }
    }
}

感谢您的任何帮助。

问候。

帮助需要者。

- 编辑 -

作品!谢谢!

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

此外,我必须确保每次运行程序时都会创建新文件!

另外,我必须确保关闭我的 FileStreams,因为我一直在崩溃程序。

4

2 回答 2

2

您忘记执行循环。book.display 只显示一本书。你也忘记读入这本书的数据。

您可以通过查看来检查文件中是否有更多数据。如果peek返回 -1,您就知道没有更多数据了。

例子:

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

while (read.PeekChar() != -1)
{
    book.readDataFrom(read);
    book.display();
}
于 2011-10-20T03:51:24.540 回答
1

使用binaryReader.PeekChar()方法。

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

编辑:

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();
    }
于 2011-10-20T03:50:33.283 回答