none
Unable to read beyond the end of the stream Error RRS feed

  • Question

  • private void button1_Click(object sender, EventArgs e)
            {
                fs = new FileStream(Directory.GetCurrentDirectory() + "\\FTask.dat", FileMode.Append);
                BinaryWriter writer = new BinaryWriter(fs);

                writer.Write(txtName.Text);
                writer.Write(txtNickName.Text);

                if (rdoMale.Checked == true)
                    writer.Write("Male");
                else
                    writer.Write("Female");

                writer.Write(txtAge.Text);
                writer.Write(dtpBirthDate.Value.ToShortDateString());
                writer.Write(txtEmail.Text);
                writer.Write(txtPhone.Text);
                writer.Write(cbxStatus.SelectedItem.ToString());
                writer.Close();

    }

    private void frmOrganizer_Load(object sender, EventArgs e) { fs = new FileStream(Directory.GetCurrentDirectory() + "\\FTask.dat", FileMode.Open); BinaryReader reader = new BinaryReader(fs); while (fs.Position < fs.Length) { txtName.Text = reader.ReadString(); txtNickName.Text = reader.ReadString(); if (reader.ReadBoolean() == true) rdoMale.Checked = true; else rdoFemale.Checked = true; txtAge.Text = reader.ReadString(); dtpBirthDate.Value = Convert.ToDateTime(reader.ReadString()); txtEmail.Text = reader.ReadString(); txtPhone.Text = reader.ReadString(); cbxStatus.SelectedItem = reader.ReadString(); } reader.Close(); }




    • Edited by Eslam Nano Saturday, June 15, 2013 3:32 PM
    Saturday, June 15, 2013 3:24 PM

Answers

  • I do not think you have to use a while loop, but you must write and read the exact kind of data in correct order.

    For writing try this:

    writer.Write(txtName.Text);
    writer.Write(txtNickName.Text);
    writer.Write(rdoMale.Checked);
    writer.Write(txtAge.Text);
    writer.Write(dtpBirthDate.Value.ToBinary());
    writer.Write(txtEmail.Text);
    writer.Write(txtPhone.Text);
    writer.Write(cbxStatus.SelectedItem.ToString());

    For reading, remove the while line (or replace with if, which checks that the file is not empty) and try this:

    txtName.Text = reader.ReadString();
    txtNickName.Text = reader.ReadString();
    rdoMale.Checked = reader.ReadBoolean();
    txtAge.Text = reader.ReadString();
    dtpBirthDate.Value = DateTime.FromBinary(reader.ReadInt64());
    txtEmail.Text = reader.ReadString();
    txtPhone.Text = reader.ReadString();
    cbxStatus.SelectedItem = reader.ReadString();

    (If cbxStatus does not contain strings, then it has to be saved in different manner. Show some details about cbxStatus items if the last line does not work).


    Sunday, June 16, 2013 7:39 AM

All replies

  • You didn't really explain the problem, but each time round the loop in the Load event handler, you are reading seven Strings and one Boolean, and you only check for the end of the stream before you go around the loop again.  If you are reading beyond the end of the stream, it must be that the last time round the loop there are less than seven Strings and one Boolean left.

    Saturday, June 15, 2013 4:24 PM
  • Try this code instead which is more robust.  Your main problem is you are writing the Male/Female using write string  and then reading using BinaryRead.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    namespace WindowsFormsApplication1
    {
        enum States
        {
            NAME,
            NICK_NAME,
            AGE,
            EMAIL,
            PHONE,
            STATUS,
        }
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            
     
            private void button1_Click(object sender, EventArgs e)
            {
                StreamWriter fs;
                fs = new StreamWriter(Directory.GetCurrentDirectory() + "\\FTask.dat", true);
                fs.WriteLine(txtName.Text);
                fs.WriteLine(txtNickName.Text);
                if (rdoMale.Checked == true) fs.WriteLine("Male");
                else fs.WriteLine("Female"); fs.WriteLine(txtAge.Text);
                fs.WriteLine(dtpBirthDate.Value.ToShortDateString());
                fs.WriteLine(txtEmail.Text);
                fs.WriteLine(txtPhone.Text);
                fs.WriteLine(cbxStatus.SelectedItem.ToString());
                fs.Flush();
                fs.Close();
            }
            private void frmOrganizer_Load(object sender, EventArgs e)
            {
                StreamReader fs;
                States state = States.NAME;
                fs = new StreamReader(Directory.GetCurrentDirectory() + "\\FTask.dat");
                while (fs.EndOfStream == false)
                {
                    string inputLine = fs.ReadLine();
                    switch (state)
                    {
                        case States.NAME:
                            txtName.Text = inputLine;
                            state = States.NICK_NAME;
                            break;
                        case States.NICK_NAME:
                            txtNickName.Text = inputLine;
                            state = States.AGE;
                            break;
                        case States.AGE:
                            dtpBirthDate.Value = Convert.ToDateTime(inputLine);
                            state = States.EMAIL;
                            break;
                        case States.EMAIL:
                            txtEmail.Text = inputLine;
                            state = States.PHONE;
                            break;
                        case States.PHONE:
                            state = States.STATUS;
                            txtPhone.Text = inputLine;
                            break;
                        case States.STATUS:
                            cbxStatus.SelectedItem = inputLine;
                            state = States.NAME;
                            break;
                    }
                         
                }
                fs.Close();
            }
        }
    }


    jdweng

    Saturday, June 15, 2013 4:57 PM
  • Thank you  joel engineer  for your response

    but i want fix my code or how fix my code

    i used a text box to write and read by binary file and it doesn't work

    i hope you understand me


    Saturday, June 15, 2013 6:05 PM
  • I do not think you have to use a while loop, but you must write and read the exact kind of data in correct order.

    For writing try this:

    writer.Write(txtName.Text);
    writer.Write(txtNickName.Text);
    writer.Write(rdoMale.Checked);
    writer.Write(txtAge.Text);
    writer.Write(dtpBirthDate.Value.ToBinary());
    writer.Write(txtEmail.Text);
    writer.Write(txtPhone.Text);
    writer.Write(cbxStatus.SelectedItem.ToString());

    For reading, remove the while line (or replace with if, which checks that the file is not empty) and try this:

    txtName.Text = reader.ReadString();
    txtNickName.Text = reader.ReadString();
    rdoMale.Checked = reader.ReadBoolean();
    txtAge.Text = reader.ReadString();
    dtpBirthDate.Value = DateTime.FromBinary(reader.ReadInt64());
    txtEmail.Text = reader.ReadString();
    txtPhone.Text = reader.ReadString();
    cbxStatus.SelectedItem = reader.ReadString();

    (If cbxStatus does not contain strings, then it has to be saved in different manner. Show some details about cbxStatus items if the last line does not work).


    Sunday, June 16, 2013 7:39 AM