What's the best way to read/write array contents from/to binary files in C#?

Posted by Eric on Stack Overflow See other posts from Stack Overflow or by Eric
Published on 2010-04-08T14:31:49Z Indexed on 2010/04/08 14:33 UTC
Read the original article Hit count: 387

Filed under:
|
|

I would like to read and write the contents of large (> 2GB), raw volume files (e.g. MRI scans). These files are just a sequence of e.g. 32 x 32 x 32 floats so they map well to 1D arrays. I would like to be able to read the contents of the binary volume files into 1D arrays of e.g. float or ushort (depending on the data type of the binary files) and similarly export the arrays back out to the raw volume files.

What's the best way to do this with C#? Read/Write them 1 element at a time with BinaryReader/BinaryWriter? Read them piece-wise into byte arrays with FileStream.Read and then do a System.Buffer.BlockCopy between arrays (keeping in mind that I want > 2GB arrays)? Write my own Reader/Writer?

© Stack Overflow or respective owner

Related posts about c#

Related posts about binaryfiles