4 Replies - 16580 Views - Last Post: 13 February 2014 - 06:12 PM Rate Topic: -----

#1 codecudder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 23-November 13

Unable to read beyond the end of the stream error

Posted 13 February 2014 - 11:24 AM

Once it gets to string name = fileWrite.ReadString(); in write file it throws this exception about unable to read beyond the end of the stream. I do not know what this means or how to begin fixing it. Any help would be appreciated, thanks!

 static void Main(string[] args)
        {

            WriteFile();
            ReadFile();
           

        }
        public static void WriteFile()
        {

            Stream myStream = File.OpenWrite("people.dat");
            BinaryWriter fileRead = new BinaryWriter(myStream);
           string name = "Sally";
           string country = "NY";
           char turnMode = 'L';
           char attackMode = 'A';
           decimal cost = 1000;
           int maxDamage = 20;

            fileRead.Write(name);
            fileRead.Write(country);
            fileRead.Write(turnMode);
            fileRead.Write(attackMode);
            fileRead.Write(cost);
            fileRead.Write(maxDamage);
            fileRead.Close();
            myStream.Close();

            Console.WriteLine("Data Written");
          
          

        }
        public static void ReadFile()
        {
           String[] files = Directory.GetFiles(".");

            
           foreach (String fileTemp in files)
            {
                if (fileTemp.Contains("people"))
                {

                    Stream myStream2 = File.OpenRead("people1.txt");
                    BinaryReader fileWrite = new BinaryReader(myStream2);
                   string name = fileWrite.ReadString();
                    string country = fileWrite.ReadString();
                    char turnMode = fileWrite.ReadChar();
                    char attackMode = fileWrite.ReadChar();
                    decimal cost = fileWrite.ReadDecimal();
                    int maxDamage = fileWrite.ReadInt16();

                   Console.WriteLine("Name" + name);
                    Console.WriteLine("Country" + country);
                    Console.WriteLine("Turn Mode" + turnMode);
                    Console.WriteLine("Attack Mode" + attackMode);
                    Console.WriteLine("Cost" + cost);
                    Console.WriteLine("Max Damage" + maxDamage);


                    fileWrite.Close();
                    myStream2.Close();

                   
                    
                }
            }


        }







Is This A Good Question/Topic? 0
  • +

Replies To: Unable to read beyond the end of the stream error

#2 Mezius   User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 72
  • Joined: 13-April 09

Re: Unable to read beyond the end of the stream error

Posted 13 February 2014 - 11:51 AM

I don't think I've ever had occasion to use a binary read/write...

Your issue is likely related to this one. http://stackoverflow...-the-stream-why

The marked answer on this one mentions "3) In the commented line below, you call the method BinaryReader.ReadString(). Please be aware that this only works if the string has been written prefixed with a compressed integer describing its length. If data were not written the same way (they usually aren't), the results will be at the very least unpredictable."

http://social.msdn.m...?forum=netfxbcl

This post has been edited by Mezius: 13 February 2014 - 11:55 AM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Unable to read beyond the end of the stream error

Posted 13 February 2014 - 03:29 PM

Opening the file for write:
Stream myStream = File.OpenWrite("people.dat");


Opening the file for read:
Stream myStream2 = File.OpenRead("people1.txt");


Notice any difference? Could be the problem.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Unable to read beyond the end of the stream error

Posted 13 February 2014 - 05:02 PM

My comment here has nothing to do with your problem; it's more about general standards. So changing this won't make your program work, but it could make you a better programmer: Why would you think it's OK for something that only writes to be named fileRead, and the other way around? Names don't matter to the computer, but they certainly do to the reader. Anyone reading your code would be forgiven for getting confused, because it's confusing! You should make sure to follow best practices whenever you can, and the #1 best practice is descriptive variable names. In your case, they're descriptive, but describing the wrong operation!
Was This Post Helpful? 0
  • +
  • -

#5 codecudder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 23-November 13

Re: Unable to read beyond the end of the stream error

Posted 13 February 2014 - 06:12 PM

Okay thank you everyone for your input. I appreciate it and now it is working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1