Click here to Skip to main content
15,891,682 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...
C#
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>
Posted

1 solution

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:\"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900