Questions about streams
Hello everyone! So recently i've been learning about streams and I have a couple of questions.
1. BinaryWriter
When I do this :
I get this in my .dat file: messagemessage2¢+ŠE
If I do this :
I get a the number : 1936026887 and then it crashes and tells me "unable to read beyond the end of stream."
Now, my understanding is that int takes 4 bytes. So i'm guessing what happens here is that I write 2 strings and it stores it as bytes, then when it reads it since it's an int it'll read 4 bytes. I'm guessing each string was 2 bytes each so it read the 2 strings as one int when I told it to therefore leaving no room for reading another string. What I don't get is, when I open the file it shows "messagemessage2¢+ŠE". What's going on there? Is it "encoding" my string when I write it? If so, what does it encode it with? Unicode? But how can it read 1936026887 when I try reading an in32? It says "messagemessage". How can "messagemessage" be converted into an int? And What does ¢+ŠE mean?
2. BinaryFormatter
So I can do this:
When I go into the CarClass1.dat file , all I see is jibberish. This is what's in the CarClass1.dat file :
" ÿÿÿÿ OWindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null WindowsFormsApplication1.Car LitersFuel "
That doesn't look like bytes AT ALL.
So now I do this :
Basically what I did is check where there was a difference between the first and second CarClass files, and where there was I just changed the value to 30 so when I deserialized it to car3 it's litersfuel field was then 30.
I have 2 questions; 1. The value 10 was stored in one byte, what if the value was larger than 255 ? Couldn't a byte not handle that? Why does it store one value in one byte then? 2. If the CarClass1.dat didn't look like a byte AT ALL, how could I store it in a bytes array, change one byte, store the newly changed byte array in a file then deserialize it into a car3 class just fine? I'm really confused by all of this so hopefully someone could come by and enlighten me.
1. BinaryWriter
When I do this :
Code:
FileInfo file = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\binarywriter.dat");
using (BinaryWriter writer = new BinaryWriter(file.OpenWrite()))
{
string message = "message";
string message2 = "message2";
writer.Write(message);
writer.Write(message2);
}
If I do this :
Code:
using (BinaryReader reader = new BinaryReader(file.OpenRead()))
{
MessageBox.Show(reader.ReadInt32().ToString());
MessageBox.Show(reader.ReadString());
}
Now, my understanding is that int takes 4 bytes. So i'm guessing what happens here is that I write 2 strings and it stores it as bytes, then when it reads it since it's an int it'll read 4 bytes. I'm guessing each string was 2 bytes each so it read the 2 strings as one int when I told it to therefore leaving no room for reading another string. What I don't get is, when I open the file it shows "messagemessage2¢+ŠE". What's going on there? Is it "encoding" my string when I write it? If so, what does it encode it with? Unicode? But how can it read 1936026887 when I try reading an in32? It says "messagemessage". How can "messagemessage" be converted into an int? And What does ¢+ŠE mean?
2. BinaryFormatter
So I can do this:
Code:
Car car1 = new Car() { LitersFuel = 10 };
Car car2 = new Car() { LitersFuel = 20 };
Car car3;
FileInfo file1 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass1.dat");
FileInfo file2 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass2.dat");
FileInfo file3 = new FileInfo(@"C:\Users\User\Documents\C# Stream Tests\CarClass3.dat");
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream stream1 = file1.OpenWrite())
using(FileStream stream2 = file2.OpenWrite())
{
formatter.Serialize(stream1, car1);
formatter.Serialize(stream2, car2);
}
" ÿÿÿÿ OWindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null WindowsFormsApplication1.Car LitersFuel "
That doesn't look like bytes AT ALL.
So now I do this :
Code:
Byte[] bytes1 = File.ReadAllBytes(file1.FullName);
byte[] bytes2 = File.ReadAllBytes(file2.FullName);
for (int i = 0; i < bytes1.Length; i++)
{
if (bytes1[i] != bytes2[i])
MessageBox.Show("Byte #" + i + " : " + bytes1[i] + " V.S " + bytes2[i]);
}
// Showed Byte #157 : 10 V.S 20
Byte[] bytes3 = bytes1;
bytes3[157] = 30;
File.WriteAllBytes(file3.FullName, bytes3);
using (FileStream stream3 = file3.OpenRead())
{
car3 = formatter.Deserialize(stream3) as Car;
}
MessageBox.Show(car3.LitersFuel.ToString());
Basically what I did is check where there was a difference between the first and second CarClass files, and where there was I just changed the value to 30 so when I deserialized it to car3 it's litersfuel field was then 30.
I have 2 questions; 1. The value 10 was stored in one byte, what if the value was larger than 255 ? Couldn't a byte not handle that? Why does it store one value in one byte then? 2. If the CarClass1.dat didn't look like a byte AT ALL, how could I store it in a bytes array, change one byte, store the newly changed byte array in a file then deserialize it into a car3 class just fine? I'm really confused by all of this so hopefully someone could come by and enlighten me.
