468,496 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

simple file compression program

Dear all,

the following is the file compression program ,using elimination of
spaces, which I saw in a book

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char * argv[])
{

FILE* fs,*ft;

fs = fopen(argv[1],"r");
if(fs == NULL)
{
printf("\n Cannot open the file %s",argv[1]);
exit(1);
}

ft = fopen(argv[2],"w");
if(fs == NULL)
{
printf("\n Cannot open the file %s",argv[2]);
exit(1);
}

while( (ch=fgetc(fs)) != EOF)
{

if(ch == 32)
{
if( (ch=fgetc(fs)) != EOF)
fputc(ch+127,ft);
}
else
fputc(ch,ft);

}

fclose(fs);
fclose(ft);

return EXIT_SUCCESS;
}

Now my questions are as as follows

1) Is there any other simpler method to compress text files, similar
to the above program(Other than standard algorithms like huffman,LZW)
Mar 26 '08 #1
3 8598
On Mar 26, 3:09 pm, sophia <sophia.ag...@gmail.comwrote:
if(ch == 32)
{
if( (ch=fgetc(fs)) != EOF)
fputc(ch+127,ft);
}
else
fputc(ch,ft);
What happens when the character represented by the value 32 is the
last character in the file? You are not writing any representation of
that character to your output file. You will not be able to recreate
your source file.
Now my questions are as as follows

1) Is there any other simpler method to compress text files, similar
to the above program(Other than standard algorithms like huffman,LZW)
yes. Not really a C issue however. First define what you mean by 'text
file', then devise a way of mapping the (smaller) domain of your text
file into the (larger) domain of an unsigned char. And don't forget to
open your destination file for binary access.
Mar 26 '08 #2

"sophia" <so**********@gmail.comwrote in message
1) Is there any other simpler method to compress text files, similar
to the above program(Other than standard algorithms like huffman,LZW)
squnch compression. It's a sliding dictionarty method that has seen
induistrial use because of its super-fast decompress. Look in the Basic
Algorithms pages of my website.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 26 '08 #3
On Wed, 26 Mar 2008 13:09:35 -0700 (PDT), sophia
<so**********@gmail.comwrote:
>Dear all,

the following is the file compression program ,using elimination of
spaces, which I saw in a book
Was it listed as a bad example? Perhaps the book was intended as a
satire?
>
#include<stdio.h>
#include<stdlib.h>

int main(int argc,char * argv[])
{

FILE* fs,*ft;

fs = fopen(argv[1],"r");
How does the program know argv[1] is not NULL or for that matter that
it even exists?
if(fs == NULL)
{
printf("\n Cannot open the file %s",argv[1]);
exit(1);
}

ft = fopen(argv[2],"w");
if(fs == NULL)
{
printf("\n Cannot open the file %s",argv[2]);
exit(1);
}

while( (ch=fgetc(fs)) != EOF)
Where is ch declared?
>{

if(ch == 32)
32 is not the value of ' ' on my system.
{
if( (ch=fgetc(fs)) != EOF)
fputc(ch+127,ft);
On my system adding 127 to a printable character value will produce a
value that won't fit in a char. While this technically isn't overflow
since fputc takes an int, it will mess up the output file.

It appears to skip only one space. And it does so without regard to
whether the space is "significant".
}
else
fputc(ch,ft);

}

fclose(fs);
fclose(ft);

return EXIT_SUCCESS;
}

Now my questions are as as follows

1) Is there any other simpler method to compress text files, similar
to the above program(Other than standard algorithms like huffman,LZW)

Remove del for email
Mar 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Enrique | last post: by
8 posts views Thread by Dennis Hotson | last post: by
1 post views Thread by Phil Price | last post: by
20 posts views Thread by Chris LaJoie | last post: by
52 posts views Thread by paytam | last post: by
6 posts views Thread by Jim M | last post: by
59 posts views Thread by riva | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.