将多个用户输入存储到.txt和.bin C#中

lebronjamesthegoat

我的班级在这里有一段代码,我在其中输入要写入.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();
                    }
                    }


            }

谢谢

哈尔多

您快要准备好了,但是代码存在一些问题,可以进行一些整理。

  1. 您正在创建3个文件:outFile.bin,outText.txt和test.txt。没有数据/文本被写入outFile.txt,它被打开,然后在几行后关闭。查看test.txt,它将具有最后的学生数据(请参阅下一点)。
  2. 您正在循环写入文件,这将覆盖数据,因此只有最后一个用户数据才被写入文件。使用outputFile = new StreamWriter("test.txt", true)此重载StreamWriter将允许您选择追加到文件(true)还是覆盖(false)。
  3. 建议您在using()写入文件或任何实现的类时查看该语句IDisposable例如,打开StreamWriter时,您可以执行using (var outputFile = new StreamWriter("test.txt", true)) { }这确保了流写入器已被垃圾收集器关闭并处置(例如,您没有关闭dataOutput文件流,如果文件流处于using(){}块中,它将自动为您处理)
  4. 可以将其重构为仅使用一个try {} catch {}块,每次写入文件时都不需要单独的try / catch。

Docs中BinaryWriter该类

将二进制类型的原始类型写入流,并支持以特定编码写入字符串。

因此,二进制写程序正在以二进制形式写,但是它所写的数据是字符串。它可以很容易地写intdoublebool,等。

这是您的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);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将多个输入存储到数组中

如何将EditText用户输入存储到c#中的字符串变量中

将虚拟环境中的 bin、include 和 lib 目录放在哪里到开发存储库中?

HEVC:从输入bin流中获取输入宽度和高度

C#将新的Textblock对象bin到XAML文件中的Textblock控件

基于动态用户输入对postgresql中存储过程的检查多个和条件

将.txt文件存储到结构中

如何将用户提示(输入)存储到数组中并检索其最大值和最小值?

将“目标计算机上的文件系统”复制到bin / Debug和bin / Release

如何通过对话框中的用户输入将数据值存储到数组中?

在C中访问和运行/ usr / bin程序

测量 bin 中的出现次数和组

如何使用 C# 和 XAML 将 QR 扫描文本结果输入到条目/编辑器中?

如何将 Rails 中的用户输入存储到 csv 文件

将用户输入的值存储到C中的字符指针?

验证存储在.txt文件中的用户名和密码以登录PHP

读取 .txt 文件中的用户名和密码

在R中读取和命名多个.txt文件

在JTextArea中打开,编辑和保存文本到.txt文件

我正在尝试将文件夹中的所有 .bin 文件转换为 Python 中的 .txt 文件

将多个行表存储到数组中以允许用户更新

C#枚举...和用户输入

Python ..将bin / hex文件转换为txt文件

每当用户输入无效的登录凭据时,如何使用PHP将用户使用的IP地址和浏览器类型存储到文本文件中?

如何在Windows7中使用批处理文件将.txt连接到.bin文件

Python写txt。将固定的页眉和页脚合并到 txt 中的可变数据

将txt文件存储到链接列表中

在bin / bash中更改用户

将多个值存储到字典中