Jonathan Crispe

Jonathan Crispe

  • NA
  • 46
  • 48.4k

Using binary writer and reader

Oct 31 2011 9:44 PM

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.


the generate button is working...but i don't know how to save and load using binaryfiles...

    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)
        {
            try
            {
                FileStream fs = new FileStream("Test.dat", FileMode.Create, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(fs);
                foreach (char cValue in textBox1.Text)
                    bw.Write(cValue);
                bw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ICA 9");
            }
        }

        private void BTN_load_Click(object sender, EventArgs e)
        {
            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, "ICA 9");
            }
        }
    }
}



Answers (1)