Daniel Lip
Daniel Lip

Reputation: 11341

How can i write and read using a BinaryWriter?

I have this code wich is working when writing a binary file:

using (BinaryWriter binWriter =
                                new BinaryWriter(File.Open(f.fileName, FileMode.Create)))
                            {
                                for (int i = 0; i < f.histogramValueList.Count; i++)
                                {

                                    binWriter.Write(f.histogramValueList[(int)i]);



                                }
                                binWriter.Close();
                            }

And this code to read back from the DAT file on the hard disk:

fileName = Options_DB.get_histogramFileDirectory();
            if (File.Exists(fileName))
            {
                BinaryReader binReader =
                    new BinaryReader(File.Open(fileName, FileMode.Open));
                try
                {
                    //byte[] testArray = new byte[3];
                    int pos = 0;
                    int length = (int)binReader.BaseStream.Length;

                    binReader.BaseStream.Seek(0, SeekOrigin.Begin);

                    while (pos < length)
                    {
                        long[] l = new long[256];

                        for (int i = 0; i < 256; i++)
                        {
                            if (pos < length)
                                l[i] = binReader.ReadInt64();
                            else
                                break;

                            pos += sizeof(Int64);
                        }
                        list_of_histograms.Add(l);
                    }
                }

                catch
                {
                }
                finally
                {
                    binReader.Close();
                }

But what i want to do is to add to the Writing code to write to the file more three streams like this:

binWriter.Write(f.histogramValueList[(int)i]);
binWriter.Write(f.histogramValueListR[(int)i]);
binWriter.Write(f.histogramValueListG[(int)i]);
binWriter.Write(f.histogramValueListB[(int)i]);

But the first thing is how can i write all this and make it in the file it self to be identify by a string or something so when im reading the file back i will be able to put each List to a new one ?

Second thing is how do i read back the file now so each List will be added to a new List ? Now it's easy im writing one List reading and adding it to a List. But now i added more three Lists so how can i do it ?

Thanks.

Upvotes: 1

Views: 1800

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

To get answer think about how to get number of items in list that you've just serialized.

Cheat code: write number of items in collection before items. When reading do reverse.

writer.Write(items.Count());
// write items.Count() items.

Reading:

int count = reader.ReadInt32();
items = new List<ItemType>();
// read count item objects and add to items collection.

Upvotes: 2

Related Questions