473,698 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading from a binary file

Hi,

how can i read input from a data file which contains binary or hex
values.
I have to use the exact binary data for further processing.only
limited number of bits are taken form the file(64 bits) for current
processing.Once the processing is done the next 64 bits must be
applied.Can this be done?

Jan 18 '06 #1
4 2461
jesuraj wrote:
how can i read input from a data file which contains binary or hex
values.
I think I know what you mean, but let's just be more precise: you don't
mean that the file is a text file that contains strings of characters
representing binary or hex digits, right? For example, your file would
*not* look like this:

0x12345678 0xC0FFEE 0xDEADBEEF

or like this:

111011101010101 0011101b 1111110000000b

IOW, when you say "binary," you mean the data is not in text mode,
right? If so, you shouldn't refer to it as binary or hex "values",
since the base of the number only affects the representation, not the
value. Rather, refer to it as "binary data" or "data in binary format".
I have to use the exact binary data for further processing.only
limited number of bits are taken form the file(64 bits) for current
processing.Once the processing is done the next 64 bits must be
applied.Can this be done?


Assuming that (1) you mean that you need to open the file in binary
mode, (2) by "must be applied" you mean "must be read and processed",
and (3) 64 is evenly divisible by the number of bits in a char, you can
do it like this:

#include <fstream>
#include <climits>
#include <cassert>
using namespace std;

int main()
{
// Is 64 evenly divisible by the no. of bits in a char?
assert( 0 == 64 % CHAR_BIT );
std::ifstream file( "file.bin", std::ios_base:: binary );
while( file )
{
char buffer[ 64 / CHAR_BIT ];
if( file.read( buffer, sizeof( buffer ) ) )
{
// Do processing of 64 bits here ...
}
}
}

You could also read larger chunks (or the whole file) into memory at
once and then process that buffer, but you didn't ask about that. You
may also want to use something other than a char buffer, especially if
your platform has a 64-bit type, and you will probably want more
sophisticated error handling. But that gives you an idea.

Cheers! --M

Jan 18 '06 #2
I have to use the exact binary data for further processing.only
limited number of bits are taken form the file(64 bits) for current
processing.Once the processing is done the next 64 bits must be
applied.Can this be done?


No.

.... just joking...

std::istream is(filename, std::ios::modeO pen|std::ios::t ypeBinary);
while(is.good() )
{
char i64[64];
is.read(&i64, 64);
}
is.close();
Jan 18 '06 #3
jesuraj wrote:
how can i read input from a data file which contains binary or hex
values.
Using 'read' or 'get'
[...]


V
Jan 18 '06 #4
Gernot Frisch wrote:
I have to use the exact binary data for further processing.only
limited number of bits are taken form the file(64 bits) for current
processing.Once the processing is done the next 64 bits must be
applied.Can this be done?
No.

... just joking...

std::istream is(filename, std::ios::modeO pen|std::ios::t ypeBinary);


You don't need the modeOpen (which I presume is obscure shorthand for
"in" or "out"), since ifstreams are always "in".
while(is.good() )
More canonical would be:

while( is )
{
char i64[64];
The OP said 64-bit, not 64-byte, which is presumably your intent here.
is.read(&i64, 64);
}
is.close();


Informational: closing of the file is automatic thanks to the
destructor and should be omitted unless there's a good reason to close
it early (which there may or may not be in the OP's code).

Cheers! --M

Jan 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6417
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of points = 123 @@@begin data@@@ gibberish follows.....
20
3061
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code: --cut here-- typedef struct pkg_ { short int info; char* data;
6
3790
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am trying to read a flatfile. In COBOL, my simple file layout and READ statement would look like below. Question: what is the standard, simple coding convention for reading in a flatfile - one record at a time?? SCANF does not work because of...
50
4947
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
7
6059
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
30
4572
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
5267
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
9879
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I should loop 15,870,080 times. The code is: newprogram.cpp =============
3
2825
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while (!file.eof ()) { do {
6
3526
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.