该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我把整个程序都贴上来了,不用大家全部看,按照顺序,读几句就到了,全贴上来是为了程序的完整性。我输入的是:abc.txt a.txt
#define DIC_SIZE 1125
#define TRUE 1
#define FALSE 0
#define END NULL
#include
#include
typedef struct dictionary{
char *character;
int code;
}DIC;
DIC string_tab[DIC_SIZE];
int count=0;
DIC *isFindDic(char *buffer);
DIC *findInDic(char *prefix);
//初始化函数
void initDic()
{
int i;
for(i=0;i<256;i++){
string_tab[i].character=i+1;
string_tab[i].code=i+1;
//printf("%c ",string_tab[i].character);
//printf("%d\n",string_tab[i].code);
count++;
}
string_tab[i+1].character=END;
}
//压缩函数
void compression(char inFileName[20],char outFileName[20])
{
char *prefix,*suffix;
FILE *inFile,*outFile;
char *buffer;
int i;
//int cIsInDic=TRUE;
DIC *curBuffer;
if(!(inFile=fopen(inFileName,"r"))){
printf("源文件打开失败!\n");
return;
}
if(!(outFile=fopen(outFileName,"w"))){
printf("目标文件建立失败!\n");
return;
}
prefix=fgetc(inFile);
suffix=fgetc(inFile);
while(!feof(inFile))
{
strcpy(buffer,prefix);//读完这句就崩溃了!!!!!!!!!!!!!!!
strcat(buffer,suffix);
if(curBuffer=isFindDic(buffer)){
fputc(curBuffer->code,outFile);
prefix=buffer;
suffix=fgetc(inFile);
}
else{
string_tab[count].character=buffer;
string_tab[count].code=count;
string_tab[++count].character=END;
curBuffer=findInDic(prefix);
fputc(curBuffer->code,outFile);
prefix=suffix;
suffix=fgetc(inFile);
}
}
} /*查找函数1
*判断当前字串是否存在于字典当中
*若存在返回结构体指针,若不存在返回FALSE
*/
DIC *isFindDic(char *buffer)
{
int i=0;
DIC *curBuffer;
while(END==string_tab[i].character){
if(string_tab[i].character==buffer){
curBuffer=&string_tab[i];
return curBuffer;
}
i++;
}
return FALSE;
}
/*查找函数2*/
DIC *findInDic(char *prefix)
{
int i=0;
DIC *curBuffer;
while(END==string_tab[i].character){
if(string_tab[i].character==prefix){
curBuffer=&string_tab[i];
return curBuffer;
}
i++;
}
} int main(int argc, char *argv[])
{
char inFileName[20],outFileName[20];
initDic();
gets(inFileName);
gets(outFileName);
compression(inFileName,outFileName);
return 0;
}