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;
}
}