?
Solved

Reading Binary data

Posted on 2006-06-23
2
Medium Priority
?
284 Views
Last Modified: 2010-04-01
I have a file of binary 1s and 0s that I want to read in chunks of size 64, for example read the 1st 64 bits into an integer array of size 64, proccess it and then read the next 64 and so on until the end of file.
How would I do this, I couldn't find a function that reads integer data from file, I tried using the function get() and cast the returned value but it gave me an array of 48s and 49s which are the ASCIIs of 1s and 0s.

Thanks in advance.
0
Comment
Question by:JessyEzzy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Expert Comment

by:DrAske
ID: 16970755
here is an idea ..

when you read the data using *getline* function ..then add if statement inside the for loop ..
for(int i =0; i<size; i++){
if(array[i] == '1')
   // process it
else
 // when it is not '1' do whatever you want ..
}
0
 
LVL 16

Accepted Solution

by:
imladris earned 320 total points
ID: 16970760
Since you are getting 48's and 49's it would appear that you have a file full of ASCII ones and zeroes. If my guess is correct then the file can be opened up with notepad, and you will see a bunch of 1's and 0's.

If so, then the file is actually a text file. One simple way to get this into an integer array might be to read each character, and then convert it to an integer in the next array element. Something like:

char bc;
int barray[64];
for(int i=0; i<64; ++i)
{   bc<<cin;
    barray[i]=(bc-'0');
}

Each text one or zero is read into a character. It is then converted to an integer by subtracting the character '0' (decimal 48) from it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question