Re: Best way to Read Binary Files

Tech-Archive recommends: Fix windows errors by optimizing your registry




One thing that's very different in C# vs C++ is that in C# you
generally do not read in a large block of data and simply assign it to
memory and then give it a type or structure. In C# you'll either use
a BinaryReader class to read in individual primitive types at a time
or use binary serialization.

You can also use either the BinaryReader or any stream to read raw
bytes into a byte array and then copy the data to appriately typed
members. With unsafe code within c# you can do direct assignment, but
that is generally avoided when not necessary.

A signed short in c++ would be a System.Int16 or "short" in c#. An
unsigned short would be System.UInt16 or "ushort".

HTH,

Sam


------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.



On Thu, 1 Feb 2007 12:47:51 -0500, "Marc Ouellette"
<admin@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi Everyone,

I am currently reading Binary files in C++Builder using the code below.
Removed some error chacking to make the code simpler to read.

std::ifstream fin;
fin.open(filename.c_str(), std::ios::in|std::ios::binary);
// read the image from file into memory
fin.read((char *)ptr, sizeof(T) * numCells);
fin.close();


I am new to C# and would like to read the same file. In the case above T is
an short.

What would be the quickest way to read it if I already know how many shorts
to read(numCells) and what is the C++ short to C# equivalent(int)?

Thanks,
Marc


.



Relevant Pages

  • Re: Interop technic withan array of Point ???
    ... C DLL or C# assembly? ... > free the Point .net structure, it would be free the equivalent memory? ... Creating an array manually and calling ... something like BinaryReader and a lot of call of ReadInt32 ...
    (microsoft.public.dotnet.framework.compactframework)
  • RE: Reading Data into a Structure in C#
    ... You have a few different options for reading in data from a binary file. ... Use BinaryReader to read in all the fields from your data structure. ... Memory map the file and play around with a byte*. ... Use XML serialization of all the data. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Whats wrong with this FTP code?
    ... Actually they're not - BinaryReader and BinaryWriter are badly named ... Reading the whole file into memory only to write it again is ... unnecessarily wasteful. ... The request stream will have to remember the ...
    (microsoft.public.dotnet.languages.csharp)