473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File IO in C

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);
}

==============================
Nov 14 '05 #1
2 1860
On Fri, 2 Apr 2004 11:35:28 +0800, "Ginrai" <gi****@hotmail.com>
wrote:
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>>
Nov 14 '05 #2

"Ginrai" <gi****@hotmail.com> wrote in message

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.
Nov 14 '05 #3

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

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
0
by: troutbum | last post by:
I am experiencing problems when one user has a document open through a share pointing to the web site. I use the dsolefile to read the contents of a particular directory and then display them in a...
0
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.