0

I am using methods to serialize and de-serialize objects to binary file that I have found described and discussed here on stackoverflow. among others in following link: Pulling objects from binary file and putting in List<T> The serialisation process is working fine, I see the file growing when objects are appended to it and when reading with deserialization the problem is that I only get the part of the list that was written in the first write to file. The data retrieved seem correct, but I do not get to read beyond the first number of objects that were in the list in the first "write" operation. it is like the file position is always set to zero.

can the read operation be looped or controlled in any way to forced it to read the entire file?

Appreciate your comments on this

Thor

  class singleBlob
    {
        public Bitmap bmpIndividualFish;
        public Int64 idNumber;
        public float area;
        public float areaRatio; 
    }

    class writeToFile 
    {

        public void write( List<singleBlob> listOfBlobs)
        {
            string dir = @"D:\temp";
            string serFileName = Path.Combine(dir, "fIndividualFish.bin");
            
            //serialize
            using (Stream streamw = File.Open(serFileName, FileMode.Append))
            {
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                bformatter.Serialize(streamw, listOfBlobs);
                //streamw.Close(); 
            }

        }

        public List<singleBlob> read()
        {
            string dir = @"D:\temp";
            string serFileName = Path.Combine(dir, "fIndividualFish.bin");

            //deserialize
            using (Stream stream = File.Open(serFileName, FileMode.Open,FileAccess.Read))
            {
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                List<singleBlob> listOfBlobs = (List<singleBlob>)bformatter.Deserialize(stream);

                //stream.Close();
                return listOfBlobs; 
            }
        }

   
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Thor P
  • 11
  • 3

1 Answers1

0

I solved the issue after reading a post from Fateme Shirmohammadi on Stackoverflow.

The solution is to check where in the stream the "reader" is and loop until it reaches the end or length of the stream. In each iteration you append the range to the list. See the changed read() method attached;

   public List<singleBlob> read()
    {
        string dir = @"D:\temp";
        string serFileName = Path.Combine(dir, "fIndividualFish.bin");

        //deserialize
        using (Stream stream = File.Open(serFileName, FileMode.Open,FileAccess.Read))
        {
            var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            List<singleBlob> listOfBlobs = new List<singleBlob>(); 

            while (stream.Position != stream.Length)
            {
                listOfBlobs.AddRange(( List<singleBlob>) bformatter.Deserialize(stream));
            }
            // List<singleBlob> listOfBlobs = (List<singleBlob>)bformatter.Deserialize(stream);

            //stream.Close();
            return listOfBlobs; 
        }

I should give credit to ladenedge and Marc Gravell who answered to Fateme's questions. Stackoverflow is rely a great resource.

Thor P

Community
  • 1
  • 1
Thor P
  • 11
  • 3