如何在文件中打印一个位而不是字节? [英] How can I print a bit instead of byte in a file?
问题描述
我使用huffman算法开发一个文件压缩器,现在我面临的问题是:
通过使用算法到字:
stackoverflow,我得到以下结果:
a,c,e,f,k,l,r,s,t ,v,w = 1次重复
o = 2次重复
a,c,e,f,k,l,r,s,t,v,w = 7.69231%
和
o = 15.3846%
所以我开始插入二叉树给我的结果:
o = 00
a = 010
e = 0110
c = 0111
t = 1000
s = 1001
w = 1010
v = 1011
k = 1100
f = 1101
r = 1110
l = 1111
这意味着树中字符的路径,考虑0为左和1到右。 p>
那么单词stackoverflow将是:
100110000100111010011111000010110110111011011111001010
整数值变成一个二进制文件,以位为单位,这将导致47位,这将发生6个字节,但相反我只能使它47bytes,因为使用fwrite或fprintf放入一个文件的最小是1字节,通过使用sizeof ($)
比我的问题是:我怎么才能在我的文件中打印一个位?
只需将头写入文件:位数,然后填充位到字节填充最后一个。这是一个示例。
#include< stdio.h>
FILE * f;
/ *当前字节多少位* /
int bit_counter;
/ *当前字节* /
unsigned char cur_byte;
/ *写1或0位* /
void write_bit(unsigned char bit)
{
if(++ bit_counter == 8)
{
fwrite(& cur_byte,1,1,f);
bit_counter = 0;
cur_byte = 0;
}
cur_byte<< = 1;
cur_byte | = bit;
}
int main()
{
f = fopen(test.bits,w);
cur_byte = 0;
bit_counter = 0;
/ *写这里的位数来解码比特流(在你的情况下为47)* /
/ * int num = 47; * /
/ * fwrite(num,1,4,f); * /
write_bit(1);
write_bit(0);
write_bit(0);
/ * etc ... - 在每个编码字符的循环中这样做* /
/ * 100110000100111010011111000010110110111011011111001010 * /
if(bit_counter> 0)
{
//用零替换最后一个字节
cur_byte <= 8 - bit_counter;
fwrite(& cur_byte,1,1,f);
}
fclose(f);
return 0;
}
要做完整的霍夫曼编码器,你必须写位码当然是开始。
I am using huffman algorithm to develop a file compressor and right now I am facing a problem which is:
By using the algorithm to the word: stackoverflow, i get the following result:
a,c,e,f,k,l,r,s,t,v,w = 1 time repeated
o = 2 times repeated
a,c,e,f,k,l,r,s,t,v,w = 7.69231%
and
o = 15.3846%
So I start inserting then into a Binary Tree, which will get me the results:
o=00
a=010
e=0110
c=0111
t=1000
s=1001
w=1010
v=1011
k=1100
f=1101
r=1110
l=1111
which means the path for the character in the tree, considering 0 to be left and 1 to right.
then the word "stackoverflow" will be: 100110000100111010011111000010110110111011011111001010
and well, I want to put that whole value into a binary file to be in bits, which will result in 47bits, which would happen to be 6bytes, but instead I can only make it 47bytes because the minimun to put into a file with fwrite or fprintf is 1byte, by using sizeof(something).
Than my question is: how can I print in my file only a single bit?
Just write the "header" to the file: the number of bits and then "pack" the bits into bytes padding the last one. Here's a sample.
#include <stdio.h>
FILE* f;
/* how many bits in current byte */
int bit_counter;
/* current byte */
unsigned char cur_byte;
/* write 1 or 0 bit */
void write_bit(unsigned char bit)
{
if(++bit_counter == 8)
{
fwrite(&cur_byte,1,1,f);
bit_counter = 0;
cur_byte = 0;
}
cur_byte <<= 1;
cur_byte |= bit;
}
int main()
{
f = fopen("test.bits", "w");
cur_byte = 0;
bit_counter = 0;
/* write the number of bits here to decode the bitstream later (47 in your case) */
/* int num = 47; */
/* fwrite(num, 1, 4, f); */
write_bit(1);
write_bit(0);
write_bit(0);
/* etc... - do this in a loop for each encoded character */
/* 100110000100111010011111000010110110111011011111001010 */
if(bit_counter > 0)
{
// pad the last byte with zeroes
cur_byte <<= 8 - bit_counter;
fwrite(&cur_byte, 1, 1, f);
}
fclose(f);
return 0;
}
To do the full Huffman encoder you'll have to write the bit codes at the beginning, of course.
这篇关于如何在文件中打印一个位而不是字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!