473,569 Members | 2,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary file input

I need to know how to input bits from a *.wav file.

I have tried this:
#include <fstream>
....
ifstream fin;
fin.open("/root/foofile.wav", ios::in|ios::bi nary);

and then inputting individual bits as a bool, and then joining them as an
int (which represents one 8 bit byte) like this (I don't care about leading
zeroes):tin

int byte=0;
bool bit;
for(int i=0; i<8; i++)
{
bit=fin.get();
if(bit)
byte*=10;
byte++;
}tin

but somehow, it's not working. Can anyone find some error in the way I'm
inputting my bits, opening my file, etc?

I am running Slackware Linux, and I use gcc(++) to compile stuff.
Jul 22 '05 #1
6 2069
"Nick R" <Nu************ **@yahoo.com.au > wrote...
I need to know how to input bits from a *.wav file.

I have tried this:
#include <fstream>
...
ifstream fin;
fin.open("/root/foofile.wav", ios::in|ios::bi nary);

and then inputting individual bits as a bool, [...]


There is [usually] no way to read individual bits from
a file. The minimal portion you must be able to read
is a byte/char.

Victor
Jul 22 '05 #2
If I read the individual byte w/ fin.get() as an int, what kind of info might I
get? 8 1s and 0s in binary, a decimal number, or what?

That is, would it recognize the file as something like:
212343464677 (dec)
where a byte would be 3 digits, 000 to 255

or seomthing like
8c9f12fea2 (hex)
where 2 digits represents a byte

or something like:
232352746516 (oct)
where 4 digits represent one byte

or something like:
100011101101010 100110, where the only way to read in info is 8 digits at a time
(1 byte)

or waht?
Jul 22 '05 #3
"CWadd64208 " <cw********@aol .com> wrote...
If I read the individual byte w/ fin.get() as an int, what kind of info might I get? 8 1s and 0s in binary, a decimal number, or what?
On the system you're using probably 8 bits (check CHAR_BIT value)

That is, would it recognize the file as something like:
212343464677 (dec)
where a byte would be 3 digits, 000 to 255

or seomthing like
8c9f12fea2 (hex)
where 2 digits represents a byte

or something like:
232352746516 (oct)
where 4 digits represent one byte

or something like:
100011101101010 100110, where the only way to read in info is 8 digits at a time (1 byte)

or waht?

Jul 22 '05 #4
Nick R <Nu************ **@yahoo.com.au > wrote in message news:<JmJHb.305 64$PK3.15499@ok epread01>...
I need to know how to input bits from a *.wav file.

I have tried this:
#include <fstream>
...
ifstream fin;
fin.open("/root/foofile.wav", ios::in|ios::bi nary);

and then inputting individual bits as a bool, and then joining them as an
int (which represents one 8 bit byte) like this (I don't care about leading
zeroes):tin

int byte=0;
bool bit;
for(int i=0; i<8; i++)
{
bit=fin.get();
if(bit)
byte*=10;
byte++;
}tin

but somehow, it's not working. Can anyone find some error in the way I'm
inputting my bits, opening my file, etc?

I am running Slackware Linux, and I use gcc(++) to compile stuff.


Well, you could always read a string and then process it all...
Jul 22 '05 #5
CWadd64208 wrote:
If I read the individual byte w/ fin.get() as an int, what kind of
info might I get? 8 1s and 0s in binary, a decimal number, or what?
You get a char value with the data. What you get out of that depends on
how you interpret it.
That is, would it recognize the file as something like:
212343464677 (dec)
where a byte would be 3 digits, 000 to 255

or seomthing like
8c9f12fea2 (hex)
where 2 digits represents a byte

or something like:
232352746516 (oct)
where 4 digits represent one byte

or something like:
100011101101010 100110, where the only way to read in info is 8 digits
at a time (1 byte)

or waht?


All the above are just different ways to print the data.

Jul 22 '05 #6
Nick R wrote:
I need to know how to input bits from a *.wav file.

I have tried this:
#include <fstream>
...
ifstream fin;
fin.open("/root/foofile.wav", ios::in|ios::bi nary);

and then inputting individual bits as a bool, and then joining them as an
int (which represents one 8 bit byte) like this (I don't care about leading
zeroes):tin

int byte=0;
bool bit;
for(int i=0; i<8; i++)
{
bit=fin.get();
if(bit)
byte*=10;
byte++;
}tin
Is "tin" a typo?
but somehow, it's not working. Can anyone find some error in the way I'm
inputting my bits, opening my file, etc?

I am running Slackware Linux, and I use gcc(++) to compile stuff.


Why are you representing a byte with an int? Because bytes are such
fundamental units, they have their own type, misleadingly named "char".
The name is because characters generally are represented by integers,
through a mapping called a "character set." Similarly, "fin.get( )"
will *not* return a boolean 1 or 0, as you might expect; instead, it
will return the integer that represents the corresponding character in
your local character set. You can represent such values intuitively
with single quotes, as in '1' and '0'.

I notice you are multiplying by ten. That's how base-ten numerals are
left-shifted. You aren't reading base ten, are you? You're reading
base two. Because this happens to be the base on which most computers
are built, and because left-shifting is such a common operation, this
task has its very own operator in C++. It looks just like the insertion
operator "<<" you may have seen used with output streams.

Also, what purpose does "bit" serve? Is it just to hold a value between
one line and the next? Lots of people do that when they think it makes
the code clearer, but I don't think it does here. However, may I
suggest you at least move the definition of "bit" down to the point of
initialization? A similar recommendation applies to the opening of the
fstream; why didn't you initialize it properly, rather than calling
"fin.open" manually on the next line? Either way, you don't have to
specify ios::in; it is implied by the fact that you are constructing an
"ifstream", not just an "fstream".

By the way, are you running this program as root? You probably don't
want to do that. However, if you try running this program as a normal
user, it may not be able to read the file from root's home directory.

Here's what I think you want. This is a very naive implementation,
since it does not check for erros in the input stream. For example, if
the file cannot be read, or if the input contains a '2' instead of a '1'
or a '0', this program will not notice.

#include <iostream>

struct Byte
{
typedef unsigned char Value;

Value value;

operator unsigned char const ( ) const { return value; }
};

std::ostream&
operator << ( std::ostream& out, Byte const& b )
{
Byte::Value value = b.value;

int i = std::numeric_li mits< Byte::Value >::digits;

while( --i >= 0 )
{
out << ( ( b.value & ( 1 << i ) ) ? '1' : '0' );
}

return out;
}

std::istream&
operator >> ( std::istream& in, Byte& b )
{
Byte::Value value = ( in.get( ) == '1' );

int i = std::numeric_li mits< Byte::Value >::digits;

while( --i )
{
value = ( value << 1 ) | ( in.get( ) == '1' );
}

b.value = value;

return in;
}
#include <fstream>

int main( int argc, char** argv )
{
Byte byte;

while( *++argv )
{
std::ifstream fin( *argv, std::ios::binar y );

fin >> byte;
std::cout << byte << '\n';
}
}

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
15210
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large,...
7
728
by: Arnold | last post by:
I need to read a binary file and store it into a buffer in memory (system has large amount of RAM, 2GB+) then pass it to a function. The function accepts input as 32 bit unsigned longs (DWORD). I can pass a max of 512 words to it at a time. So I would pass them in chunks of 512 words until the whole file has been processed. I haven't worked...
103
48552
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things,...
28
2753
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
12
5854
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and...
8
8920
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using GetChunk and AppendChunk to read and write binary data from the OLE Object field. I am using VBA and DAO for this experiment. The OLE Object field...
68
5180
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
7
19205
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
3
3831
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into the two problems while trying to read a binary file. 1). All whitespace characters were skipped 2). Certain binary files gave a core dump
1
3592
by: yohan610 | last post by:
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are no errors... but when i print out the binary data from the input file it seems to be the same whtever the file, and then after encrypting and wirting...
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.