这里有我班上的一段代码,我在其中输入要写入.txt和.bin文件的值。
我的问题是我的.txt文件最终为空并且我的.bin文件未以二进制形式写入。
我相信我已经正确编写并关闭了.bin文件和.txt,但是我的输入没有正确存储
这是我的代码
static void Main(string[] args)
{
string numberOfStudents, studentName, studentHeight, studentWeight;
int studentInput = 0, height;
bool numberOutput, heightOutput, weightOutput;
double weight;
Console.Write("Enter number of students: ");
numberOfStudents = Console.ReadLine();
numberOutput = int.TryParse(numberOfStudents, out studentInput);
if (numberOutput == true)
{
if (studentInput <= 0)
{
Console.WriteLine("Number of students must be a positive integer (greater than 0)!");
Console.ReadKey();
}
else
{
for (int i = 1; i <= studentInput; i++)
{
Console.Write("Enter student name: ");
studentName = Console.ReadLine();
Console.Write("Enter student height in centimetres: ");
studentHeight = Console.ReadLine();
heightOutput = int.TryParse(studentHeight, out height);
Console.Write("Enter student weight in kilograms: ");
studentWeight = Console.ReadLine();
weightOutput = double.TryParse(studentWeight, out weight);
try
{
StreamWriter outputFile;
outputFile = new StreamWriter("test.txt");
outputFile.Write(numberOfStudents + studentName + studentHeight + studentWeight);
outputFile.Close();
}
catch (System.IO.IOException exc)
{
Console.WriteLine("There was an error!: " + exc.Message);
}
try
{
FileStream outputFile = new FileStream("outFile.bin", FileMode.Create);
BinaryWriter BinFile = new BinaryWriter(outputFile);
BinFile.Write(studentName + " " + studentHeight + " " + studentWeight);
BinFile.Close();
}
catch (System.IO.IOException exc)
{
Console.WriteLine("There was an error!: " + exc.Message);
}
FileStream dataOutput = new FileStream("Database", FileMode.Create);
BinaryWriter databaseFile = new BinaryWriter(dataOutput);
StreamWriter textOutput = new StreamWriter("outText.txt");
databaseFile.Write(studentName + " " + studentHeight + " " + studentWeight);
databaseFile.Close();
textOutput.Close();
}
}
}
谢谢
答案 0 :(得分:0)
您快到了,但是代码有一些问题,可以解决一下。
outFile.txt
,先打开然后关闭几行。在test.txt
中查找,它将获得最后的学生数据(请参阅下一点)。outputFile = new StreamWriter("test.txt", true)
,StreamWriter
的重载将允许您选择追加到文件(true)还是覆盖(false)。using()
的任何类时查看IDisposable
语句。例如,打开StreamWriter时,您可以执行using (var outputFile = new StreamWriter("test.txt", true)) { }
。这确保了流写入器已被垃圾收集器关闭和处置(例如,您没有关闭dataOutput
文件流,如果它位于using(){}
块中,则会自动为您处理)< / li>
try {} catch {}
块,每次写入文件时都不需要单独的try / catch。 从Docs到BinaryWriter
类
将二进制类型的原始类型写入流中,并支持以特定编码写入字符串。
因此,二进制编写器正在以二进制形式写入,但是它正在写入的数据是字符串。可以轻松编写int
,double
,bool
等。
这是您的try / catch块中的代码整理了一下(这只会创建test.txt和outFile.bin):
try
{
// will open or create file in 'append' mode
using (var outputFile = new StreamWriter("test.txt", true))
{
outputFile.Write($"{numberOfStudents}, {studentName}, {studentHeight},{studentWeight}");
outputFile.Write(Environment.NewLine);
}
using (BinaryWriter databaseFile = new BinaryWriter(File.Open("outFile.bin", FileMode.Append)))
{
databaseFile.Write(studentName + " " + studentHeight + " " + studentWeight);
}
}
catch (System.IO.IOException exc)
{
Console.WriteLine("There was an error!: " + exc.Message);
}