如何保存和加载(二进制文件) [英] how to save and load ( binary files )

查看:157
本文介绍了如何保存和加载(二进制文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下保存按钮时,将使用SaveDialog选择用于二进制文件的文件名.使用bin的默认扩展名,并对Binary文件(* .bin)和All文件(*.*)进行过滤.使用MessageBox显示遇到的任何错误,将数组的内容保存到选定的文件.

当按下 Load 按钮时,OpenDialog将用于选择用于读取二进制文件的文件名.使用bin的默认扩展名,并对Binary文件(* .bin)和All文件(*.*)进行过滤.使用MessageBox从所选文件中读取数组的内容,以显示遇到的任何错误.请注意,必须创建双精度数组以更正长度,才能从文件中读取所有双精度.清除先前的内容,然后使用上面显示的格式将读入数组的双打显示在多行文本框中.显示从文件读取的值的总和.

我不知道为什么加载不起作用...

When the Save button is pressed, the SaveDialog will be used to select a filename to be used for a binary file. Use a default extension of bin, and a filter for Binary files (*.bin) and All files (*.*). Save the contents of the array to the selected file, using a MessageBox to display any errors encountered.

When the Load button is pressed, the OpenDialog will be used to select a filename to be used to read a binary file. Use a default extension of bin, and a filter for Binary files (*.bin) and All files (*.*). Read the contents of the array from the selected file, using a MessageBox to display any errors encountered. Note that the array of doubles must be created to correct length to read all of the doubles from the file. Clear out the previous contents, then display the doubles read into the array to the multiline textbox using the format shown above. Display the sum of the values read in from the file.

i don''t know why load is not working...

public partial class Form1 : Form
{
    Random rNumber = new Random();

    public Form1()
    {
        InitializeComponent();

    }

    private void BTN_generate_Click(object sender, EventArgs e)
    {
        int total = rNumber.Next(50, 1001);
        List<double> numbers = new List<double>();

        for (int i = 0; i < total; i++)

            numbers.Add(Math.Round(0.0 + rNumber.NextDouble() * 100.0, 2));
        textBox1.Text = String.Join(", ", numbers.Select(s => s.ToString()).ToArray());

        double dtotal = numbers.Sum();
        LBL_sum.Text = dtotal.ToString("F");
    }

    private void BTN_save_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "Binary Files (*.bin)|*.*|All fIles (*.*)|*.*";
        saveFileDialog1.ShowDialog();
        FileStream fs;

        try
        {
            fs = new FileStream("Test.dat", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write("C:");
            bw.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "ICA 9");
        }

    }

    private void BTN_load_Click(object sender, EventArgs e)
    {

        openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "Binary Files (*.bin)|*.*|All fIles (*.*)|*.*";
        long iNumInts = 0;
        int[] iArray = null;

        try
        {
            FileStream fs = new FileStream("Test.dat", FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            iNumInts = fs.Length / sizeof(int);
            iArray = new int[iNumInts];
            for (int i = 0; i < iNumInts; i++)
                iArray[i] = br.ReadInt32();
            br.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "BinaryReaderExample");
        }
    }
}</double></double>

推荐答案

对于初学者来说,您不是在BTN_load_Click中调用"openFileDialog1.ShowDialog();"
另外,除了字符串"C:\"
For starters, you''re not calling ''openFileDialog1.ShowDialog();'' in BTN_load_Click

Also, you''re not saving anything to the file except the string "C:\"


这篇关于如何保存和加载(二进制文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆