473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to use filestream.seek to read something using deserialization

In this code, I write two ints to a binary file using Serialization. What I want to do is after writing them, read in only the second int. I should be able to just seek to 4 bytes past the beginning of the file, since each int is 4 bytes, but this won't work. Am I wrong in my understanding of seek?

Expand|Select|Wrap|Line Numbers
  1. FileStream serializeStream = new FileStream("test.bin", FileMode.Create);
  2. BinaryFormatter bf = new BinaryFormatter();
  3.  
  4. int firstNum = 1111;
  5. int secondNum = 2222;
  6.  
  7. serializeStream.Seek(0, SeekOrigin.Begin);
  8. bf.Serialize(serializeStream, firstNum);
  9. bf.Serialize(serializeStream, secondNum);
  10. serializeStream.Close();
  11.  
  12. FileStream retrieveStream = new FileStream("test.bin", FileMode.Open);
  13. retrieveStream.Seek(4, SeekOrigin.Begin);
  14. int num= (int)bf.Deserialize(retrieveStream);
  15. Console.WriteLine("Number: " + num);
  16. retrieveStream.Close();
Jan 25 '09 #1
3 4562
mldisibio
190 Expert 100+
I'm not sure if you chose BinaryFormatter because you thought you were writing data to a file in binary format, or for other reasons. I'm pretty sure the Serialize method will add metadata to the stream describing the object it is serializing - in this case a simple int, but an object that lives in an assembly nonetheless.
To confirm this, simply open the output file and see if it is longer than 8 bytes for the two ints.

I think what you might want is BinaryWriter Class (System.IO). This will write simple types directly to file, and you can then Seek and call ReadInt32() at the appropriate file position with a System.IO.BinaryReader.
Jan 26 '09 #2
mldisibio
190 Expert 100+
Otherwise, to use BinaryFormatter as you are doing, you need to store each serialized object in its own stream and read the entire stream into the Deserializer to reconstitute the underlying Int32 object.
Serialization is a way to store an entire object as bits, and deserialization is a way to read those bits and reconstitute the object.
It is not just the value of the object stored as bits, which is your assumption when you navigate to the fifth byte thinking it is the value of the second integer.
An Int32 represents four bytes of data in memory - which a BinaryReader/Writer understands.
Int32 is also an object with methods and properties - which is what serialization cares about.
Jan 26 '09 #3
Thank you. I'm not very familiar with writing to binary, but I understand what I need to do now.
Jan 26 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Nadav | last post by:
Hi, I am using FileStream's Async API: BeginRead/EndRead, upon completion callback execution I use the read data and call EndRead, Taking that in mind, I Wonder... does calling EndRead will cause...
3
by: sbparsons | last post by:
I have a file opened as a FileStream. I have a StreamReader and StreamWriter object opened, referencing the FileStream object. My aim is to read lines from the file until I find the line where...
0
by: James Johnson | last post by:
I have a file that I open a FileStream and a StreamReader for. I issue a SEEK and read a series of records. I then issue another SEEK, no errors, but when I issue the read it picks up where the...
9
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory...
0
by: David | last post by:
I have a VB.NET 1.1 app that uses FileStream to read files. It opens a file, reads about 4K "header" data bytes from the beginning of it, then does a Seek into the file (the distance of the Seek...
9
by: Drayshak | last post by:
Hi, I'm currently making an application which reads a file and extracts information from it. Whole files are embedded in this single file, and information about each file that is embedded is...
7
by: Piotrekk | last post by:
Hi Probability of this phenomena is like 1/200. The only code where I am dealing with file is the following: using (FileStream fs = new FileStream(this.FilePath,...
4
by: DR | last post by:
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write...
0
by: Mark Salsbery [MVP] | last post by:
"DR" <softwareengineer98037@yahoo.comwrote in message news:Op0NyzR8IHA.1200@TK2MSFTNGP04.phx.gbl... If the filestream is opened for write access then it still keeps its write access. If you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.