Solved

Reading Binary data

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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