473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Best way to Read Binary Files

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
2 2988
Hi,

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
1
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
11
by: jean-jeanot | last post by:
I can access to a file with the command: file_obj = open ( " D:\My documents\Textfile.txt",'r') When I now try to read a file with the following command: file_obj = open ("D:\My...
3
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
12
by: MrQuan | last post by:
G'day all, I have a requirement to communicate between two or more PCs over the Internet, however I have no idea how to go about this. I'm not talking about a chat programme as such, I want to...
6
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100...
0
by: Marc Ouellette | last post by:
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;...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.