⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.c

📁 哈夫曼压缩程序
💻 C
字号:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<direct.h>
#include <windows.h>

#include"file.h"
#include"N_error.h"
#include"compress.h"

char *LinkPath_Name(char *path,char *filename)
{
	int i = 0;
	for(i = 0;path[i];i++);
	if(path[i-1] != '\\')
	{
		path[i] = '\\';
		path[i+1] = 0;
	}
	if(filename[0] == '\\')
		filename++;
    return strcat(path,filename);
}

FILE *OpenF(const char *filename,char *opentype,char *errormsg)
{
	FILE *fp;
	if(opentype[0] == 'w')
		CreateFolders(filename);
	if( (fp = fopen(filename,opentype)) == NULL)
	{
		N_ERROR(errormsg);
		return 0;
	}
	return fp;
}


int WriteC(char c,FILE *fp,char *errormsg)
{
	if(fwrite(&c,1,1,fp) != 1)
	{
		N_ERROR(errormsg);
		return 0;
	}
	else return 1;
}

int WriteLIntToF(long int a,FILE *fp)
{
	int i = 0;
	char *cp = (char*)&a;
	for(i = sizeof(long int)-1 ;i >= 0;i--)
	{
		if(!WriteC(*(cp+i),fp,"At function compress Write file error"))
			return 0;
	}
	return 1;
}

int WriteStrToF(char *str,FILE *fp)
{
	int i = 0;
	for(;str[i] != 0;i++)
	{
	if( !WriteC(str[i],fp,"At WriteStrToF error when write the string!"))
		return 0;
	}
	WriteC(0,fp,"At WriteStrToF error when write the string!");
	return 1;
}

 int ReadC(FILE *fp)
{//if read end or error return -1;
	unsigned char ch;
	if(fread(&ch,1,1,fp) == 0) return -1;
	return (unsigned int)ch;
}

f_name_t CreateFileName()
{
	f_name_t r;
	r.head = 0;
	return r;
}

f_name_node_p F_N_GoNext(f_name_node_p p)
{
	if(p)
	{
		return p->Next;
	}
	else
	{
		N_ERROR("at function F_N_GoNext can not go next when it's the end of file name node");
		exit(0);
	}
}

char *F_N_GetName(f_name_node_p p)
{
	if(p)
	{
		return p->Name;
	}
	else
	{
		N_ERROR("at function F_N_GetName can not get name from empty node of file name node ");
		exit(0);
	}
}

int AddFileName(f_name_p filenamelist,const char *filename)
{
	f_name_node_p N;
	f_name_node_p *pp;
	char *p;
	if( !(N = (f_name_node_p)CallSpace(sizeof(*N))))
	{
		N_ERROR("at function AddFileName can not call space!");
		return 0;
	}

	if( !(p = (char *)CallSpace(sizeof(*p)*strlen(filename)+1)))
	{
		N_ERROR("at function AddFileName can not call space!");
		return 0;
	}
	
	

	N->Name = strcpy(p,filename);
	N->Next = 0;
	pp = &filenamelist->head;

	N->Next = *pp;
    *pp = N;
	return 1;
}

void FreeFileList(f_name_p names)
{
	f_name_node_p p1,p2;
	p1 = names->head;
	while( p1 )
	{
		p2 = p1;
		p1 = p1->Next;
		free(p2->Name);
		free(p2);
	}
}

int CreateFolders(const char *FileName){	char temp[256] = {0},*chp,*p;	if(FileName[0] == '\\')		FileName++;	p = temp;	while( strcpy(temp,FileName), chp = strchr(p,'\\'))	{		*chp = 0;		if( (_mkdir(temp) != 0)&&(errno == ENOENT))				N_ERROR("at function CreateFolders can't create folder");		p = chp + 1;	}	return 1;}///////////////////////////////////////////////////////////////////////////////void FindFileRecur(char *SearchPath,char *Name,char *Last,f_name_p namesp){	WIN32_FIND_DATA FileData; 
	HANDLE hSearch; 
	char *cpPath,*cpName;
	BOOL fFinished = FALSE; 
	//save the path for next use
	cpPath = SearchPath+strlen(SearchPath)-2;
	cpName = Name + strlen(Name);

	SearchPath[strlen(SearchPath)-2] = 0;//delete the "\\*"

	//add the last folder(or file name) name to SearchPath
	if(Last&&Last[0])
	{
		strcat(SearchPath,"\\");
		strcat(Name,"\\");
	}
	strcat(SearchPath,Last);	 strcat(Name,Last);
	strcat(SearchPath,"\\*");	

	hSearch = FindFirstFile(SearchPath, &FileData); 
	if (hSearch == INVALID_HANDLE_VALUE) 
	{ 
		SearchPath[strlen(SearchPath)-2] = 0;//delete the "\\*"
		//add the filename found
		AddFileName(namesp,Name);
	}

	else 
		while(FindNextFile(hSearch, &FileData))
		{
			if(strcmp(FileData.cFileName,"..") &&strcmp(FileData.cFileName,".")) 
				FindFileRecur(SearchPath,Name,FileData.cFileName,namesp);
		}

	//revert SearchPath
	strcpy(cpPath,"\\*");
	*cpName = 0;

	FindClose(hSearch);}void AddFileFromFolder(char* FolderPath,char *FolderName,f_name_p namesp){	char Name[MAX_PATH] = {0},SearchPath[MAX_PATH] = {0};	strcpy(SearchPath,FolderPath);	LinkPath_Name(SearchPath,FolderName);	strcat(SearchPath,"\\*");	strcpy(Name,FolderName);	FindFileRecur(SearchPath,Name,"",namesp);}/////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -