代码之家  ›  专栏  ›  技术社区  ›  Sss

c#中泛型类型<T>出现未处理的异常,因为文件大小不是字节的倍数

  •  1
  • Sss  · 技术社区  · 11 年前

    我是 C# 初学者和试图读取二进制文件来计算 frequency symbols 内部 binary file 。(频率是符号重复的次数)。

    作为第一步,我保留了 data type 属于 "symbol" 读为 "int" 代码运行良好。 但现在我想让这个符号 generic type (我是说 <T> 类型)。

    该代码在Ubuntu终端中编译时没有任何错误。

    但当我使用 "mono filename.exe BinaryFile.bin" 此二进制文件的读取位置为 sole argument .请最后看看我是怎么得到这个二进制文件的 二进制文件.bin

    错误是:(我知道这个错误的原因,我在下面提到过,但不知道原因。)

       hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ mono cbk.exe  toto.bin 
    
    Unhandled Exception: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
      at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
      at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
      at Final.A`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
      at Final.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
    [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
      at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
      at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
      at Final.A`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
      at Final.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
    hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ 
    

    我的完整代码等效于这样做(缩小的代码,它的作用相同,您可以根据需要编译它):

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace Final 
    {
        public class A <T> where T: struct, IComparable <T> , IEquatable <T> 
        {
            public class Node 
            {
                public T symbol;
                public Node next;
                public int freq;
            }
    
            public Node Front;
    
            public A(string[] args, Func <byte[], int, T> converter) 
            {    
                int size = Marshal.SizeOf(typeof (T));
                Front = null;
                using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) 
                {   
                    while (stream.BaseStream.Position < stream.BaseStream.Length) 
                    {   
                        byte[] bytes = stream.ReadBytes(size);   
                        T processingValue = converter(bytes, 0);  
    
                        Node pt, temp;
                        pt = Front;
    
                        while (pt != null) 
                        {   Console.WriteLine("check1");
                            if (pt.symbol.Equals(processingValue)) 
                            {
                                pt.freq++;
                                break;
                            }
                            Console.WriteLine("Symbol : {0}  frequency is : {1}", pt.symbol, pt.freq);
                            temp = pt;
                            pt = pt.next;
                        }
                    }
                }
            }
        }
    
        public class MyClass 
        {
            public static void Main(string[] args) 
            {
                A <long> ObjSym = new A <long> (args, BitConverter.ToInt64);
            }
        }
    }
    

    我认为问题是在创建Huffman类型的对象时产生的 public class MyClass 。有人能帮我解决这个问题吗?谢谢 重要提示:我知道为什么会出现此未处理的异常错误。这正是由于此Object声明:

    A <long> ObjSym = new A <long> (args, BitConverter.ToInt64);
    

    但如果我这样做,就没有任何错误。

    A <short> ObjSym = new A <short> (args, BitConverter.ToInt16);
    

    但它给出了错误的输出(因为 一起读取2个字节,但我希望每次只读取1个字节 ). 错误消失,错误是因为如果是BitConverter.ToInt16,此文件必须是16的倍数,如果是BitTransformer.ToInt64,则此文件必须为64的倍数。

    为了从这个二进制文件中获得正确的输出,我必须有如下内容: A ObjSym=新A(参数,BitConverter.ToInt8);

    这是因为字节的大小是“8位”,我必须读取每个“8位 但我在C#中没有找到可以容纳8位的数据类型,例如,我对BitConverter.Toint16保持“短”,对BitConverter.Toint64保持“长”,还有其他可能。有人能告诉我这是“8位等效整数类型”吗 会有很大的帮助。谢谢

    1 回复  |  直到 11 年前
        1
  •  1
  •   rschmidt    11 年前

    问题在于:

    byte[]bytes=stream.ReadBytes(大小);

    当方法stream.ReadBytes读取<8字节,则byte[]数组具有长度<8,在方法BitConverter.ToInt64(byte[]value,int startIndex)中出现错误。