开发者

BinaryReader missing values

Im using theBinaryReader to place the elements of a binary file into a List of Int16. But the count value of elements read back from the binary file is incorrect. Here is the Reader:

            using (var file = File.OpenRead(openFileName))
            using (var reader = new BinaryReader(file))
            {
                int count = reader.ReadInt16();
                    this.view.Data = new List<Int16>(count);
                for (int i = 0; i < count; i++)
                    this.view.Data.Add(reader.ReadInt16());
            }

I know that when im saving my binary file that the view.Data list constains 19000 elements, but then I look at the count value during the read method shown above its telling me that count is only 134 :(

here is the writter:

                    using (var file = File.Create(fileName))
                    using (view.Writer = new Bi开发者_Python百科naryWriter(file))
                    {
                        foreach (Int16 dataItem in view.Data)
                        {
                            view.Writer.Write(dataItem);
                        }
                    }

Total file size after write is 40KB, and when in debug mode the view.Data contains 19000 when savign, and only 134 when reading?

could anyone please help


You are not writing out the count, it should be like this to match your reading code:

                using (var file = File.Create(fileName))
                using (view.Writer = new BinaryWriter(file))
                {
                    Int16 count = (Int16) view.Data.Count;
                    view.Writer.Write(count);
                    foreach (Int16 dataItem in view.Data)
                    {
                        view.Writer.Write(dataItem);
                    }
                }

Probably the first value view.Data[0] has a value of 134, that's why are you currently are only reading that many.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜