Solved

Reading Binary data

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 125
C language IDE – Compilers installation 14 71
White board coding practice 3 88
Why  my code (program) build with old compiler? 11 46
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

773 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