zoukankan      html  css  js  c++  java
  • Fast Binary File Reading with C#

    public static TestStruct FromFileStream(FileStream fs) {     //Create Buffer      byte[] buff = new byte[Marshal.SizeOf(typeof(TestStruct))];      int amt = 0;      //Loop until we've read enough bytes (usually once)       while(amt < buff.Length)         amt += fs.Read(buff, amt, buff.Length-amt); //Read bytes       //Make sure that the Garbage Collector doesn't move our buffer       GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);     //Marshal the bytes      TestStruct s =        (TestStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),        typeof(TestStruct));      handle.Free();//Give control of the buffer back to the GC       return s }
    public static TestStruct FromBinaryReaderBlock(BinaryReader br) {     //Read byte array      byte[] buff = br.ReadBytes(Marshal.SizeOf(typeof(TestStruct)));     //Make sure that the Garbage Collector doesn't move our buffer       GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);     //Marshal the bytes      TestStruct s =        (TestStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),       typeof(TestStruct));     handle.Free();//Give control of the buffer back to the GC       return s; }
    public static TestStruct FromBinaryReaderField(BinaryReader br) {      TestStruct s = new TestStruct();//New struct       s.longField = br.ReadInt64();//Fill the first field       s.byteField = br.ReadByte();//Fill the second field       s.byteArrayField = br.ReadBytes(16);//...       s.floatField = br.ReadSingle();//...       return s; }
  • 相关阅读:
    hdu--4027--不错的线段树
    hdu--3275--线段树<again>
    hdu--2795--又是线段树
    hdu--4407--一不留神就TLE了
    zoj--3822--第二题概率dp
    hdu--3911--线段树<我最近爱上她了>
    hdu--1710--二叉树的各种遍历间的联系
    hdu--1712--分组背包<如果你真的明白了背包..>
    hdu--4576--概率dp<见过最简单的概率dp>
    list remove object
  • 原文地址:https://www.cnblogs.com/free5ddvd/p/2230919.html
Copyright © 2011-2022 走看看