i dont understand why i have that exceptions. All i try to do is to write a file . Kill an object and load the file :(


it says:
"Unable to read beyond the end of the stream."

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Project1
{
    class Class1
    {
        public static void Main(string[] args)
        {
           Doctor d = new Doctor() { Age = 32, Sex = "Female" };
           Secretary s = new Secretary() { Age = 22, Sex = "Female" };
           BinaryWriter writer=new BinaryWriter(File.Create(@"J:\\testing.txt"));
           BinaryWriter writer2 = new BinaryWriter(File.Create(@"J:\\testing2.txt"));
           d.Add(writer);
           s.Add(writer2);
           writer.Close();
           writer2.Close();
           d = null;
           s = null;

           BinaryReader reader = new BinaryReader(File.OpenRead(@"J:\\testing.txt"));
           BinaryReader reader2 = new BinaryReader(File.OpenRead(@"J:\\testing2.txt"));
           Doctor.PrintD(reader);

           Secretary.PrintS(reader2);
        }

    }
}

class Doctor
{
    public int Age { get; set; }
    public string Sex { get; set; }

    public void Add(BinaryWriter b)
    {
        b.Write(Age);
        b.Write(Sex);
    }
    public static Doctor PrintD(BinaryReader r)
    {
        Doctor d = new Doctor();
        d.Age = (int)r.ReadInt64();
        d.Sex = r.ReadString(); // debugger shows that it is thrown here.Why?
        return d;

    }

}

class Secretary
{
    public int Age { get; set; }
    public string Sex { get; set; }

    public void Add(BinaryWriter b)
    {
        b.Write(Age);
        b.Write(Sex);
    }

    public static Secretary PrintS(BinaryReader r)
    {
        Secretary s = new Secretary();
        s.Age = r.ReadInt32();
        s.Sex = r.ReadString();
        return s;
    }

}

One thing that looks different is line 46:
d.Age = (int)r.ReadInt64();

You are reading a 64 bit integer but you have created it on line 35 as a 32 bit integer (int).

Change to
d.Age = (int)r.ReadInt32();

commented: Thanks blade +0

Thanks single blade , it works now. but why wont the number show up clearly in the text?

why am i being shown some weird square after the file is produced??

Thanks single blade , it works now. but why wont the number show up clearly in the text?

why am i being shown some weird square after the file is produced??

btw, why do you use a BinaryRader class, becuase you only read from a text file?
Why you dont use StreamReader instead, and you wont even need to convert byte into string, something?

An idea.
Mitja

i know the method of StreamReader. i wanted to investigate how to do it with Binary,
why does it refuse to translate it to an int..?

hmm... Did you save the data into a file in the same order then you are retreiving them back out?

the point is that you cannot just simply asign some binary data to something. You have to get the data into the text in some order, and then retreive them back in the same way:EXAMPLE!

If this is not enough its better to take a look into some other example, that you will learn how to use bytes and converted them back to the characters (strings, integers,...).

Give it some time and go through all the code slowely:
http://msdn.microsoft.com/es-es/library/system.io.binaryreader.aspx

<quote>why am i being shown some weird square after the file is produced??</quote>

The square usually means some non visual output like a new line character or tab or something similar.

Check your file and make sure there is nothing in it but the age.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;



namespace ConsoleApplication1
{
    class Program
    { 
        static void Main(string[] args)
        {
            string filename = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\test.txt";

            Person person = new Person { Name = "Ana", Age = 25, Height = 1.70 };
            Console.WriteLine(person);
            BinaryWriter bw = new BinaryWriter(File.Create(filename));
            person.Save(bw);
            bw.Close();
            person = null;
            Console.WriteLine("==========================");
            BinaryReader br = new BinaryReader(File.OpenRead(filename));
            person=  Person.Load(br);
            Console.WriteLine(person);
        }
       
    }
}
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public double Height { get; set; }
    public Person Yadid { get; set; }

    public override string ToString()
    {
        return string.Format("Name: {0} Age: {1} Height: {2}", Name, Age, Height);
    }
    public void Save(BinaryWriter bw)
    {
        bw.Write(Name);
        bw.Write(Age);
        bw.Write(Height);
    //    if(Yadid!=null)
    //        Yadid.Save(bw);
    }
    public static Person Load(BinaryReader br)
    {
        Person person = new Person();
        person.Name = br.ReadString();
        person.Age = br.ReadInt32();
        person.Height = br.ReadDouble();
       // person.Yadid = Person.Load(br);
        return person;
    }

}

Thats the teachers code. it works.
i just applied different variables and added a class. Why doesnt my code work? it follows the same principles, or does it not?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.