473,385 Members | 1,772 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,385 software developers and data experts.

Bit-by-bit Reading from Memory

3
How do you read a binary file in memory bit-by-bit?

ifstream file ("my.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);


I was using file.get() but returns the next char I really want the next bit.

Any help is appreciated. :-)
Jul 29 '08 #1
3 3712
Savage
1,764 Expert 1GB
You can only get a byte(char) when reading from the file.If the next bit is what you need you can pull it out from the returned byte using bitwise operators. Anyway why do you need to pull out next bit from the text file?
Jul 29 '08 #2
kenone
3
You can only get a byte(char) when reading from the file.If the next bit is what you need you can pull it out from the returned byte using bitwise operators. Anyway why do you need to pull out next bit from the text file?
I read in a binary file which is about 1/2 Gb to a char array. I then have to search for the first occurrance of 10101 or 111, that is the header of a message, what follows is the data of different size fields. I have found it too difficult, and cumbersome, to search for the pattern (using file.get())and then extract the fields as the pattern can start in any bit in a byte (for instance the lsb could be the start of the pattern 10101 but for the next message it could start at the 4th bit). Ideally it would be great to copy the char array to a vector<bool> then interrogating the vector would be easy, but I don't know how to do this. One message is 83 bits long and the other message is 41 bits long.

Any help would be much appreciated as I have been trying to solve this for about two weeks now and it is driving me nuts. :-)
Jul 29 '08 #3
boxfish
469 Expert 256MB
Hi,
Go through the file char by char, looping through the bits in the char and push_back()ing them into your vector<bool>. To get a particular bit out of the char, you:
create a char with a true bit in only that place, eg 00000010 for place 1, or 00100000 for place 5, and
use the bitwise & operator to compare that to your original char, eg
00100000 & 01001010
The result will not have any true bits in it, unless both chars have a bit in that place, and if the answer has any true bits in it, it will be true when you cast it to bool.
To get a char with only a bit in a particular place, you do:
1 << place
to slide 1 into the right place. You will actually get an int, but it will work fine.
So:
Expand|Select|Wrap|Line Numbers
  1. for (int place = 0; place < 8; place++)
  2.     myVector.push_back(myChar & (1 << place));
  3.  
Hope this helps.
Jul 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Scott Niu | last post by:
Hi, I have this following simple c++ program, it will produce memory leak ( see what I did below ). My observation also showed that: There will be a mem leak when all the 3 conditions are true:...
4
by: Sebastian Becker | last post by:
Hello NG, is fscanf capable of reading a memory adress from a file? I tried to find the right format-parameter, but my code doesn't seem to work... Regards, Sebastian
11
by: Yvad | last post by:
When I encounter software crash, the software always pop-up something like " The instruction at "0x1000a1eb" referenced memory at "0x000000c0". The memory could not be "read"". Then Visual C++...
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
3
by: Paminu | last post by:
I get this error after running my application for some time. What does it mean and what should I be looking for in my code?
10
by: Segfahlt | last post by:
I have a fairly simple C# program that just needs to open up a fixed width file, convert each record to tab delimited and append a field to the end of it. The input files are between 300M and...
5
by: Maxime Savard | last post by:
Hi there, What I'm trying to do is quite simple and in fact I have found a way to do it but I am not sure if it is OK. What I mean is sometime in c++ stuff can "work" but not be "correct". Here...
6
by: itsolution | last post by:
Hi folks, Could you shed some light on this issue? my program is running on Freebsd as a daemon. When user sends a request, it forks itself and lets its child process handles the request....
10
by: deciacco | last post by:
I'm writing a command line utility to move some files. I'm dealing with thousands of files and I was wondering if anyone had any suggestions. This is what I have currently: $arrayVirtualFile =...
0
by: neverMind | last post by:
Hi there, I have an assignment to invert a ppm image, with a header of 4 lines (P6\nwidth\nheight\ndepth\n),and we have to do a shared memory version and a memory mapped file (MMF) too. This...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.