首先把一些字符输入到一个文件中,然后对里面的字符进行压缩和解压 用C语言编写程序来实现...
请各位帮帮忙哈!!!谢谢.........
交作业?
曾经写过一个
http://bbs.bc-cn.net/viewthread.php?tid=82126&star=at#
把里面那个改成二进制文件处理就行了
#include <stdio.h>
#include<stdlib.h>
int reduce(char *file_name); //压缩一个文件
int dell_fill(char *file1_name,char *file2_name);//删除两个文件
int re_file(char *file1_name,char *file2_name);//解压缩两个文件
void main()
{
char *file_name="E:\\file.txt";
reduce(file_name);
puts("\nend!");
getchar();
}
int reduce(char *file_name)
{
FILE *fp,*fp1,*fp2;
long fnum=0;
int f=0,f_h=1,flag,i;
char *file1_name,*file2_name;
if((fp=fopen(file_name,"rb"))==NULL)
{
printf("Open the \"%s\" error!",file_name);
getchar();
exit(1);
}
i=0;
flag=0;
do
{
if(file_name[i]=='.')
{
file1_name[i]='a';
file1_name[i+1]='.';
file2_name[i]='b';
file2_name[i+1]='.';
flag=1;
}
else
{
file1_name[i+flag]=file_name[i];
file2_name[i+flag]=file_name[i];
}
}while(file_name[i++]!=0);
if(!flag)
{
file1_name[i]='a';
file2_name[i]='b';
}
if((fp1=fopen(file1_name,"w+"))==NULL)
{
printf("Creat the \"%s\" error!",file1_name);
getchar();
exit(1);
}
if((fp2=fopen(file2_name,"w+"))==NULL)
{
printf("Creat the \"%s\" error!",file2_name);
getchar();
exit(1);
}
f_h=fgetc(fp);
while(f!=-1)
{
f=fgetc(fp);
if(f_h==f)
{
fnum++; //解除长整形限制
}
else
{
do
{
fputc(fnum%256,fp1);
fputc(f_h,fp2);
}while(fnum/=256);
fnum=0;
}
f_h=f;
}
fclose(fp);
fclose(fp1);
fclose(fp2);
re_file(file1_name,file2_name);
dell_fill(file1_name,file2_name);
return 0;
}
int dell_fill(char *file1_name,char *file2_name)
{
unlink(file1_name);
unlink(file2_name);
return 0;
}
int re_file(char *file1_name,char *file2_name)
{
FILE *fp,*fp1,*fp2;
int f=0,f_h,i,flag;
char *file_name;
if((fp1=fopen(file1_name,"rb"))==NULL)
{
printf("Creat the \"%s\" error!",file1_name);
getchar();
exit(1);
}
if((fp2=fopen(file2_name,"rb"))==NULL)
{
printf("Creat the \"%s\" error!",file2_name);
getchar();
exit(1);
}
i=0;
flag=0;
do
{
if(file1_name[i]=='.')
{
file_name[i-1]='.';
flag=-1;
}
else
file_name[i+flag]=file1_name[i];
}while(file1_name[i++]!=0);
if(!flag)
file_name[i-1]=0;
if((fp=fopen("E:\\file.txt","wt"))==NULL)
{
printf("Creat the \"%s\" error!",file_name);
getchar();
exit(1);
}
while(f!=-1)
{
f=fgetc(fp1);
f_h=fgetc(fp2);
for(i=0;i<=f;i++)
{
fputc(f_h,fp);
}
}
fclose(fp);
fclose(fp1);
fclose(fp2);
return 0;
}
这个是你的杰作,你可以作一点注释吗???
我看不懂,怎么运行结束以后就是一个end.这样什么都没有了,
也没有实现压缩和解压的功能,还有里面的
int dell_fill(char *file1_name,char *file2_name);//删除两个文件
int re_file(char *file1_name,char *file2_name);//解压缩两个文件
这两个函数好象没有用处,不起作用.......
分析一下好吗???你觉得我将怎样改才可以啊
还有这里看不明白
do
{
if(file_name[i]=='.')
{
file1_name[i]='a';
file1_name[i+1]='.';
file2_name[i]='b';
file2_name[i+1]='.';
flag=1;
}
else
{
file1_name[i+flag]=file_name[i];
file2_name[i+flag]=file_name[i];
}
}while(file_name[i++]!=0);
if(!flag)
{
file1_name[i]='a';
file2_name[i]='b';
}
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
int reduce(char *file_name); //压缩一个文件
int dell_fill(char *file1_name,char *file2_name);//删除两个文件
int re_file(char *file1_name,char *file2_name);//解压缩两个文件
void main()
{
char *file_name="E:\\file.txt";
reduce(file_name);
puts("\nend!");
getchar();
}
int reduce(char *file_name)
{
FILE *fp,*fp1,*fp2;
long fnum=0;
int f=0,f_h=1,flag,i;
char *file1_name,*file2_name;
//打开预压缩文件
if((fp=fopen(file_name,"rb+"))==NULL)
{
printf("Open the \"%s\" error!",file_name);
getchar();
exit(1);
}
//处理压缩缓存文件名
i=0;
flag=0;
do
{
if(file_name[i]=='.')
{
file1_name[i]='a';
file1_name[i+1]='.';
file2_name[i]='b';
file2_name[i+1]='.';
flag=1;
}
else
{
file1_name[i+flag]=file_name[i];
file2_name[i+flag]=file_name[i];
}
}while(file_name[i++]!=0);
if(!flag)
{
file1_name[i]='a';
file2_name[i]='b';
}
//建立压缩缓存文件
if((fp1=fopen(file1_name,"wb+"))==NULL)
{
printf("Creat the \"%s\" error!",file1_name);
getchar();
exit(1);
}
if((fp2=fopen(file2_name,"wb+"))==NULL)
{
printf("Creat the \"%s\" error!",file2_name);
getchar();
exit(1);
}
//压缩文件
f_h=fgetc(fp);
while(f!=-1)
{
f=fgetc(fp);
if(f_h==f)
{
fnum++; //解除长整形限制
}
else
{
do
{
fputc(fnum%256,fp1);
fputc(f_h,fp2);
}while(fnum/=256);
fnum=0;
}
f_h=f;
}
//关闭文件
fclose(fp);
fclose(fp1);
fclose(fp2);
//解压文件
re_file(file1_name,file2_name);
return 0;
}
int dell_fill(char *file1_name,char *file2_name)
{
unlink(file1_name);
unlink(file2_name);
return 0;
}
int re_file(char *file1_name,char *file2_name)
{
FILE *fp,*fp1,*fp2;
int f=0,f_h,i,flag;
char *file_name;
//打开压缩缓存文件
if((fp1=fopen(file1_name,"rb+"))==NULL)
{
printf("Creat the \"%s\" error!",file1_name);
getchar();
exit(1);
}
if((fp2=fopen(file2_name,"rb+"))==NULL)
{
printf("Creat the \"%s\" error!",file2_name);
getchar();
exit(1);
}
//处理原文件名
i=0;
flag=0;
do
{
if(file1_name[i]=='.')
{
file_name[i-1]='.';
flag=-1;
}
else
file_name[i+flag]=file1_name[i];
}while(file1_name[i++]!=0);
if(!flag)
file_name[i-1]=0;
//建立原文件
if((fp=fopen("E:\\NewFile.txt","wb+"))==NULL)
{
printf("Creat the \"%s\" error!",file_name);
getchar();
exit(1);
}
//解压文件
while(f!=-1)
{
f=fgetc(fp1);
f_h=fgetc(fp2);
for(i=0;i<=f;i++)
{
fputc(f_h,fp);
}
}
//关闭文件
fclose(fp);
fclose(fp1);
fclose(fp2);
return 0;
}
哪里不懂回帖或者加我QQ说吧