473,378 Members | 1,383 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,378 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 2987
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;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...

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.