Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strike><a href="https://stackoverflow.com/questions/3206391/directly-reading-large-binary-file-in-c-w-out-copying/3206424#3206424">dtb's answer</a> is an even better way</strike> (actually, it has to copy the data as well, no gain there), but I just wanted to point out that to extract ushort values from a byte array you should be using <a href="http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx" rel="nofollow noreferrer"><code>BitConverter</code></a> not <code>BinaryReader</code></p> <p>EDIT: example code for p/invoking <a href="http://pinvoke.net/default.aspx/kernel32/ReadFile.html" rel="nofollow noreferrer">ReadFile</a>:</p> <pre><code>[DllImport("kernel32.dll", SetLastError=true)] [return:MarshalAs(UnmanagedType.Bool)] static extern bool ReadFile(IntPtr handle, IntPtr buffer, uint numBytesToRead, out uint numBytesRead, IntPtr overlapped); [DllImport("kernel32.dll", SetLastError=true)] [return:MarshalAs(UnmanagedType.Bool)] static extern bool SetFilePointerEx(IntPtr hFile, long liDistanceToMove, out long lpNewFilePointer, uint dwMoveMethod); unsafe bool read(FileStream fs, ushort[] buffer, int offset, int count) { if (null == fs) throw new ArgumentNullException(); if (null == buffer) throw new ArgumentNullException(); if (offset &lt; 0 || count &lt; 0 || offset + count &gt; buffer.Length) throw new ArgumentException(); uint bytesToRead = 2 * count; if (bytesToRead &lt; count) throw new ArgumentException(); // detect integer overflow long offset = fs.Position; SafeFileHandle nativeHandle = fs.SafeFileHandle; // clears Position property try { long unused; if (!SetFilePositionEx(nativeHandle, offset, out unused, 0); fixed (ushort* pFirst = &amp;buffer[offset]) if (!ReadFile(nativeHandle, new IntPtr(pFirst), bytesToRead, out bytesToRead, IntPtr.Zero) return false; if (bytesToRead &lt; 2 * count) return false; offset += bytesToRead; return true; } finally { fs.Position = offset; // restore Position property } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload