File IO in C

G

Ginrai

Dear all,

I would like to write a file compress and decompress program in C,
I tried to find some in internet and I figured out the below (File I/O part)

My question is why there are so many shift in bits? And I actually don't
quite understand what it is doing.

Is there any simple method just output the code calculated to a text file,
and read it again without error?

Is there so many bit shift just due to the codeword is variable length?

Thank you very much.

Ginrai

==============================

Compress:

void write_code(FILE *output,unsigned int code)
{
static int output_bit_count=0;
static unsigned long output_bit_buffer=0L;

output_bit_buffer |= (unsigned long) code <<
(32-MAXBITS-output_bit_count);
output_bit_count += MAXBITS;
while (output_bit_count >= 8)
{
putc(output_bit_buffer >> 24,output);
output_bit_buffer <<= 8;
output_bit_count -= 8;
}
}

==============================

Decompress:


unsigned int read_code(FILE *input)
{
unsigned int return_value;
static int input_bit_count=0;
static unsigned long input_bit_buffer=0L;
unsigned char ch;

while (input_bit_count <= 24)
{
ch = getc(input);
input_bit_buffer |= ch << (24-input_bit_count);
input_bit_count += 8;
}

return_value=input_bit_buffer >> (32-MAXBITS);
input_bit_buffer <<= MAXBITS;
input_bit_count -= MAXBITS;
return(return_value);
}

==============================
 
B

Barry Schwarz

Dear all,

I would like to write a file compress and decompress program in C,
I tried to find some in internet and I figured out the below (File I/O part)

My question is why there are so many shift in bits? And I actually don't
quite understand what it is doing.

Is there any simple method just output the code calculated to a text file,
and read it again without error?

Is there so many bit shift just due to the codeword is variable length?

Thank you very much.

Ginrai

You have to understand the algorithm before you can understand the
code.


<<Remove the del for email>>
 
M

Malcolm

Ginrai said:
I would like to write a file compress and decompress program in C,
Is there any simple method just output the code calculated to a text >
file, and read it again without error?A compressor / decompressor usually wants to write bits to a file. C has no
direct support for this - data must be written in whole bytes. Therefore a
readbit() / writebit() function has to be written on top of the stdio
library, usually using a lot of bit shifts.
What you can do is open a text file and just write a single bit as '1' or
'0'. Obviously you won't actually compress anything, but the size of the
file in bytes represents the size it will be in bytes. You can get your
compressor / decompressor working with this file.
As the last stage, either translate the whole file to binary at one go (less
efficient), or rewrite the load/save routines so that you are writing one
bit at a time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top