By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

Best way to Read Binary Files

P: n/a
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
Feb 1 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

The way of doing it in C# is similar, just use FileStream
--
Ignacio Machin
machin AT laceupsolutions com
Feb 1 '07 #2

P: n/a

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"
<ad***@scp-solutions.com.nospamwrote:
>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
Feb 1 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.