1. 程式人生 > >普通檔案格式與二進位制檔案格式互轉

普通檔案格式與二進位制檔案格式互轉

普通檔案與二進位制檔案間的相互轉換

普通檔案字尾為 ".txt",沒帶字尾的為二進位制檔案

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace  std;

typedef unsigned long  ULONG;

typedef struct fileStr
{
	long nId;		
	long nType;        
	long nCharge;		    
	
	fileStr()
	{
		nId = 0;
		nType = 0;
		nCharge = 0;
	}
}file_str;

ULONG m_nBlockSize = sizeof(file_str);

void ParseString(string szSrc ,const char* szSpilt,vector& vecDest)
{
	//字串分解
	return ;
}

bool checkFile(string strFileName)
{
	ULONG nSize = 0;
	
	struct stat oldStat;
	stat(strFileName.c_str(), &oldStat);
	nSize = oldStat.st_size;

	if (nSize % m_nBlockSize)
	{
		return false;
	}
	return true;
}

int BinToNorfile(string strFileName)
{
	file_str Record;
	FILE* write_fd = NULL;
	FILE* read_fd = NULL;
	char pzBuf[1024]={0};
	char pzfilename[256] = {0};
	string sfilename = "";//只有檔名,不包含路徑
	read_fd = fopen(strFileName.c_str(),"rb");
	if (NULL == read_fd)
	{
		cout<<"read file failed,or the file "<<strFileName<<" does not exist"<<endl;
		return -1;
	}
	if (checkFile(strFileName))//read.checkFile()
	{
		char pzPath[256]={0};
		int nPos = strFileName.rfind("/");
		if (string::npos == nPos)
		{
			sfilename = strFileName;
		}
		else
		{
			sfilename = strFileName.substr(nPos+1);//原始檔名,不包含路徑
			sprintf(pzPath,"%s",(strFileName.substr(0,nPos+1)).c_str());
		}
		
		sprintf(pzfilename,"%s_bak",sfilename.c_str());//寫檔案
		strcat(pzPath,pzfilename);
		write_fd = fopen(pzfilename,"a+");

		fseek(read_fd,0L,SEEK_SET);
		while(!feof(read_fd)) 
		{
			file_str st_file;
			memset(pzBuf,0x0,sizeof(pzBuf));
			fread(&st_file,sizeof(file_str),1,read_fd);
			sprintf(pzBuf,"%ld|%ld|%ld\n",st_file.nId,st_file.nType,st_file.nCharge);

			int nRet = fwrite(pzBuf,strlen(pzBuf),1,write_fd);
			if(nRet != 1)
			{
				cout<<"failed to writefile !"<<endl;
				return -1;
			}
			fseek(write_fd,0,SEEK_END);
		}
		fclose(write_fd);
	}
	else
	{
		fclose(read_fd);
		printf("Check file error, file[%s].\n", strFileName.c_str());
		return -1;
	}
	fclose(read_fd);
	return 0;
}

int NorToBinfile(string strFileName)
{
	file_str Record;
	FILE* write_fd = NULL;
	FILE* read_fd = NULL;
	char pzBuf[1024]={0};
	char pzfilename[256] = {0};
	string sfilename = "";//只有檔名,不包含路徑
	read_fd = fopen(strFileName.c_str(),"r");
	if (NULL == read_fd)
	{
		cout<<"read file failed,or the file "<<strFileName<<" does not exist"<<endl;
		return -1;
	}
	if (true)
	{
		char pzPath[256]={0};
		int nPos = strFileName.rfind("/");
		if (string::npos == nPos)
		{
			nPos = strFileName.rfind(".txt");
			sfilename = strFileName.substr(0,nPos);//去掉 .txt
		}
		else
		{
			string strTmp = strFileName.substr(nPos+1);//原始檔名,不包含路徑
			nPos = strTmp.rfind(".txt");
			sfilename = strTmp.substr(0,nPos);
			sprintf(pzPath,"%s",(strFileName.substr(0,nPos+1)).c_str());
		}

		sprintf(pzfilename,"%s",sfilename.c_str());//寫檔案
		strcat(pzPath,pzfilename);
		write_fd = fopen(pzfilename,"wb+");

		fseek(read_fd,0L,SEEK_SET);
		while(!feof(read_fd)) 
		{
			file_str st_file;
			memset(pzBuf,0x0,sizeof(pzBuf));
			fgets(pzBuf,sizeof(pzBuf),read_fd);

			if (!strcmp(pzBuf,""))
			{
				continue;
			}

			vector vecStr;
			ParseString(pzBuf,"|",vecStr);
			if (vecStr.size()>= 3)
			{
				st_file.nId = atol(vecStr[0].c_str());
				st_file.nType = atol(vecStr[1].c_str());
				st_file.nCharge = atol(vecStr[2].c_str());
			}
			else
			{
				cout<<"the file content is wrong, the file is "<<strFileName<<endl;
				return -1;
			}
			int nRet = fwrite(&st_file,sizeof(file_str),1,write_fd);
			if(nRet != 1)
			{
				cout<<"failed to writefile !"<=2)
	{
		strcpy(pzFilename,argv[1]);
	}

	if (0 == strlen(pzFilename))
	{
		cout<<"please set the filepath"<<endl;
	}
	char* pzExtent = strrchr(pzFilename,'.');
	if (NULL != pzExtent)
	{
		if (!strcmp(pzExtent,".txt"))
		{
			NorToBinfile(pzFilename);//文字檔案轉二進位制檔案
		}
		else
		{
			BinToNorfile(pzFilename);//二進位制轉文字檔案
		}
	}
	else
		BinToNorfile(pzFilename);
	

	return 0;
}