473,699 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to write bits by using ofstream?

Hi, all

I am wondering how to write bits by using ofstream?

I have finished a huffman tree, but how can I write the bits to the file in
order to gain compression?

for example, 'A' returns a code '1101', what I should write? 13? no, I don't
think so
Just now I tried my own thinking, 'cause 4 bits can represent a hex, so I
write 'D' instead of '13' in the above example,
but, I can only gain compression if there are a few unique characters, like
"abc abc abc" or "aaaaaaaaaaaaaa a"
If there are many different characters, my thinking cannot "compress" the
file.
I have worked on this for couple of days, still can't figure out what I
should write and how I can write the bits to the file in order to compress
file.
Please help, thank you very much!

--
Best Regards!
Sowen Cheung
http://com.angGoGo.com
http://www.angGoGo.com
http://biz.angGoGo.com
Jul 23 '05 #1
8 8742
"Sowen" <so***@anggogo. com> wrote...
I am wondering how to write bits by using ofstream?

I have finished a huffman tree, but how can I write the bits to the file
in order to gain compression?
You cannot write bits. The smallest amount you are allowed to write
is a char (byte). In order to write bits, you need to pack them in
a bunch of bytes and then write those bytes to the file stream.

If after your effort you have 269 bits to write, pack them up, you
will get ceil(269/CHAR_BIT) bytes (on many systems CHAR_BITS is 8,
so you get 34 bytes), and write those 34 bytes out.

This is beyond the scope of comp.lang.c++, however, but you need to
somehow identify the sequences of bits. My guess is that you should
read more on the subject of compression and how compressed information
is saved to files. Imagine that once you written those packed bits
out to a file, you will want to be able to read and unpack them in
some way. So, you cannot simply stack those bits in a large byte
array, you need extra information placed there to help with unpacking.

The Web undoubtedly contains enough information on the subject.
[...]


V
Jul 23 '05 #2
Sowen wrote:
Hi, all

I am wondering how to write bits by using ofstream?

I have finished a huffman tree, but how can I write the bits to the
file in order to gain compression?

for example, 'A' returns a code '1101', what I should write? 13? no,
I don't think so
Just now I tried my own thinking, 'cause 4 bits can represent a hex,
so I write 'D' instead of '13' in the above example, but, I can only
gain compression if there are a few unique characters, like "abc abc
abc" or "aaaaaaaaaaaaaa a" If there are many different characters, my
thinking cannot "compress" the file.


Normally, you accumulate bits until you get at least one full byte, and
then write that to the stream. In reality, one byte is also a pretty
small amount -- it's usually better to accumulate a buffer full of
bytes before writing out the data.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #3
Victor Bazarov wrote:

[ ... ]
This is beyond the scope of comp.lang.c++, however, but you need to
somehow identify the sequences of bits. My guess is that you should
read more on the subject of compression and how compressed
information is saved to files. Imagine that once you written those
packed bits out to a file, you will want to be able to read and
unpack them in some way. So, you cannot simply stack those bits in
a large byte array, you need extra information placed there to help
with unpacking.


Huffman-compressed data does not require such extra information.

The bits in a Huffman-compressed stream represent decisions in a binary
tree. As you read bits in, you use them to traverse the tree. When you
reach a leaf node in the tree, you know you've reached the end of one
symbol. The next bit is the beginning of the next compressed symbol,
with it you restart traversing the tree from the root.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #4
Hi, Jerry

Do you mean the following:

repeat
read char from inFile
get code from tree
put code in array
if length of array > 8, write to file
remove the code that has been written from the array
until inFile.eof

so, I can use maybe "unsigned char code[8]" to store 1 byte, is it right?
if I am using ofstream, I can do

outFile << ios:binary << code

like that ?

sorry, I am a little confused in here

I didn't try the code yet, so I am not sure whether it works

--
Best Regards!
Sowen Cheung
http://com.angGoGo.com
http://www.angGoGo.com
http://biz.angGoGo.com
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Sowen wrote:
Hi, all

I am wondering how to write bits by using ofstream?

I have finished a huffman tree, but how can I write the bits to the
file in order to gain compression?

for example, 'A' returns a code '1101', what I should write? 13? no,
I don't think so
Just now I tried my own thinking, 'cause 4 bits can represent a hex,
so I write 'D' instead of '13' in the above example, but, I can only
gain compression if there are a few unique characters, like "abc abc
abc" or "aaaaaaaaaaaaaa a" If there are many different characters, my
thinking cannot "compress" the file.


Normally, you accumulate bits until you get at least one full byte, and
then write that to the stream. In reality, one byte is also a pretty
small amount -- it's usually better to accumulate a buffer full of
bytes before writing out the data.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #5
ok, I found writing unsigned char array like that is wrong

could you tell me how to write 8 bits as 1 byte to the file?
thanks

--
Best Regards!
Sowen Cheung
http://com.angGoGo.com
http://www.angGoGo.com
http://biz.angGoGo.com
"Sowen" <so***@anggogo. com> wrote in message
news:d1******** **@canopus.cc.u manitoba.ca...
Hi, Jerry

Do you mean the following:

repeat
read char from inFile
get code from tree
put code in array
if length of array > 8, write to file
remove the code that has been written from the array
until inFile.eof

so, I can use maybe "unsigned char code[8]" to store 1 byte, is it right?
if I am using ofstream, I can do

outFile << ios:binary << code

like that ?

sorry, I am a little confused in here

I didn't try the code yet, so I am not sure whether it works

--
Best Regards!
Sowen Cheung
http://com.angGoGo.com
http://www.angGoGo.com
http://biz.angGoGo.com
"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Sowen wrote:
Hi, all

I am wondering how to write bits by using ofstream?

I have finished a huffman tree, but how can I write the bits to the
file in order to gain compression?

for example, 'A' returns a code '1101', what I should write? 13? no,
I don't think so
Just now I tried my own thinking, 'cause 4 bits can represent a hex,
so I write 'D' instead of '13' in the above example, but, I can only
gain compression if there are a few unique characters, like "abc abc
abc" or "aaaaaaaaaaaaaa a" If there are many different characters, my
thinking cannot "compress" the file.


Normally, you accumulate bits until you get at least one full byte, and
then write that to the stream. In reality, one byte is also a pretty
small amount -- it's usually better to accumulate a buffer full of
bytes before writing out the data.

--
Later,
Jerry.

The universe is a figment of its own imagination.


Jul 23 '05 #6
However, what Victor is saying is that you need to store that tree.

Which is half true. Yes, the decompressing algorithm needs to know the
tree, and storing the tree with the file leads to the best compression
of the actual data part. (The tree's overhead might make this untrue
for the whole.) However, there are two other choices. The first is to
use the same tree for every file, which could work well if it's always
compressing the same type of file (e.g. natural language could probably
be compressed with such a scheme almost as well as computing frequencys
and building a tree for every file). The second is to use an adaptive
Huffman algorithm, which starts at a known point and builds the tree as
the input flows in and is compressed.

Jul 23 '05 #7
Sowen wrote:
Hi, Jerry

Do you mean the following:

repeat
read char from inFile
get code from tree
put code in array
if length of array > 8, write to file
remove the code that has been written from the array
until inFile.eof
Yes, that's the rough idea.
so, I can use maybe "unsigned char code[8]" to store 1 byte, is it right? if I am using ofstream, I can do

outFile << ios:binary << code

like that ?


No -- first of all, ios::binary is used when you open a file. Second
two write out a byte, you want a single unsigned char, NOT an array of
8 of them -- that would hold 8 bytes.

So, assume your first character gives a code of 1101. You put that into
the first four bits of your byte, and then get the next code for the
next character. We'll say that gives a code of 10111 (five bits). You
put the first four of those bits into the same character as you already
have holding the bits of previous code. That gives you an 8-bit byte
that you can then write out to the file. You now have one bit as the
beginning of the next code, so you save it and read in the third input
character.

Despite its mostly deservedly being denigrated elsewhere, you might
find it convenient to use a vector<bool> to accumulate the bits before
you write them out to the file.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #8

"Jerry Coffin" <jc*****@taeus. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Sowen wrote:
Hi, Jerry

Do you mean the following:

repeat
read char from inFile
get code from tree
put code in array
if length of array > 8, write to file
remove the code that has been written from the array
until inFile.eof


Yes, that's the rough idea.
so, I can use maybe "unsigned char code[8]" to store 1 byte, is it

right?
if I am using ofstream, I can do

outFile << ios:binary << code

like that ?


No -- first of all, ios::binary is used when you open a file. Second
two write out a byte, you want a single unsigned char, NOT an array of
8 of them -- that would hold 8 bytes.

So, assume your first character gives a code of 1101. You put that into
the first four bits of your byte, and then get the next code for the
next character. We'll say that gives a code of 10111 (five bits). You
put the first four of those bits into the same character as you already
have holding the bits of previous code. That gives you an 8-bit byte
that you can then write out to the file. You now have one bit as the
beginning of the next code, so you save it and read in the third input
character.


From the original post, it looks like what's being returned in characters
'1' and '0, in some kind of string or character array. If that's the case,
then to put that information into a byte requires a little bit-twiddling.
You'll need some kind of index as to which bit in the byte is next to be
written into, and an "if" statement which sets that bit to 0 if '0' is the
next character to be packed and sets it to 1 if '1' is the next character to
be packed. (Actually, if you set the byte you're writing to to 0 each time
you start packing into it again, then you only need to set the 1 bits when
you encounter a '1'.) If you don't know how to set a bit to 1, ask again or
check out Google for some examples.

-Howard


Jul 23 '05 #9

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

Similar topics

4
10755
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group consisting of 6 numbers - with a total of 50 groups of numbers. A well-known girl that some publishing companies use to provide introductory level textbooks to various Junior Colleges in the U.S., not surprisngly, asks for this same exact...
6
4612
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to console as well as to file, my code is as below, ======================================================================= #include <iostream.h> #include<ostream> #include<sstream>
2
4236
by: randy1200 | last post by:
I have the following: short x = 3; ... std:ofstream file(outfile.txt, std::ios::out|std::ios::trunc); ... file << x << endl; Of course, "3" shows up in the file. What I'd really like to see is "00000011". I'd be very surprised to see that C++ doesn't already have an
5
18594
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
1
2194
by: kavi81 | last post by:
Hi, I have written a c++ code which is a message queue. I have an Enqueue and a Dequeue. Here is the code for enqueue: ########################################################## Enqueue ##########################################################
5
14681
by: shyam | last post by:
HI I have a logging application wherin I create a ofstream object say, ofstream logger; by doing this logger.open(/* some path*/,ios_base:app);
3
6845
by: garyatusa | last post by:
Hi, there, I need to put a bunch of data to an iostream, or a buffer sequencially for each object, and write them to a file in specific sequence. I couldn't find any helpful information how to do that. If iostream is not the best way, how to do it. I don't know the size of the object. Your time and help are highly appreciated. Following is the structure stripped off unnecessary parts: class Base
6
17179
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
9
1817
by: kk_oop | last post by:
Consider class X and Y where: class X { double d; int i; Y myY; }; class Y
0
8685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.