#include "stdlib.h"
#include "string.h"
#include "stdio.h"
FILE *infile,*outfile;
void Common(char *filename)
{
char tempspace[200];
strcpy(tempspace,"Unable to open ");
strcat(tempspace,filename);
puts(tempspace);
exit(0);
}
void Compress(char *infilename,char *outfilename)
{
register int seq_len;
char cur_char,cur_seq;
if((infile=fopen(infilename,"rb"))==NULL) //rb 二进制只读
{
Common(infilename);
}
if((outfile=fopen(outfilename,"wb"))==NULL)
{
Common(outfilename);
}
cur_char = fgetc(infile);
seq_len=1;
while(!feof(infile))
{
cur_char=fgetc(infile);
if(cur_char==cur_seq)
{
seq_len++;
}
else
{
fputc(seq_len,outfile);
fputc(cur_seq,outfile);
cur_seq=cur_char;
seq_len=1;
}
}
}
int main(int argc, char* argv[])
{
printf("rleys compress file\n");
char a[20],b[30];
char *in,*out;
printf("Input infilename\n");
scanf("%s",a); //gets(a);
printf("Input outfilename\n");
scanf("%s",b);
in=a;
out=b;
printf("\n compression...");
Compress(in,out);
fclose(infile);
fclose(outfile);
return 0;
}
c实现文件压缩
最新推荐文章于 2024-06-15 19:57:23 发布