gpt4 book ai didi

c# - 如何使用 BinaryReader 并将数据正确输入文件?

转载 作者:行者123 更新时间:2023-11-30 16:27:31 27 4
gpt4 key购买 nike

我正在做家庭作业,但完全卡住了!我想要做的是使用已经定义的输入并使用 saveDataTo() 方法将其保存到文件中,然后使用 readDataFrom() 方法读取输入。

我卡在了第一部分。我不确定是否必须先初始化 Program.cs 文件中的数据?

我不知道,我被困住了。这是代码,希望能提供一些提示,帮助我完成此任务。

-- 编辑--

我可以为 saveDataTo() 和 readDataFrom() 方法添加说明:

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.

所以这给了我一个线索,我应该使用和分配要保存在文件中的属性?

-- 编辑--

更新了那里的代码。我确实对保存到文件中的内容有疑问。我没有看到价格。这是为什么?

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

问候。

求助。

最佳答案

你问的是在 Program.cs 还是 Book.cs 中定义值,对吗?嗯,在Program.cs中定义值就可以了,你只需要在写入数据之前确保所有的值都给定了。

因此,由于该函数采用了一个假定已预先初始化的 BinaryWriter 参数,因此这应该有效:

public void saveDataTo(BinaryWriter w)
{
w.Write(getAuthorName());
//etc...
}

但是,请记住,在调用保存数据之前,您确实需要在某处(任何地方)定义所有信息。

关于c# - 如何使用 BinaryReader 并将数据正确输入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com