Curtis Schreiner

Curtis Schreiner

  • NA
  • 10
  • 38.8k

BinaryReader loop ReadString

Apr 3 2013 12:59 AM
Hey there,

I don't use binary reader a lot, but I was just curious how you can loop the data read from the BinaryReader. I have no problems just returning a single record, however if I wanted to return all of the records either through a "For" or "While" loop, I'm curious how this can be achieved? The test data file I'm working with has a set length to 27 bytes for every record that was created, but again I was just seeing how you would be able to print out all the records through a loop?

I tried a few different variations on the loop, however the end results always comes back with "Unable to read beyond the end of the stream."

Here's a sample of script below.


        private void button1_Click(object sender, EventArgs e)
        {

            string name;
            double rate;
            string record;
            int record_number, bytes, size;
            int position = 0;

            FileStream f = new FileStream(@"c:\tester.dat", FileMode.OpenOrCreate,
            FileAccess.ReadWrite);


            //Reading from the f - filestream
            BinaryReader r = new BinaryReader(f);

            //Writing to the f - filestream
            BinaryWriter w = new BinaryWriter(f);


            for (int x = 0; x < f.Length; x++)
            {
                record = r.ReadString();
                textBox4.AppendText(record);
            }


            f.Close();

Answers (1)