Solved

Reading Binary data

Posted on 2006-06-23
2
253 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
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 80 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 66
Modify static variables within a class in VC++ 3 95
SendMessage fails while PostMessage works 2 107
returning a dereferenced pts in C++ 10 141
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now