普通文件格式与二进制文件格式互转
来源:互联网 发布:js 删除table tr 编辑:程序博客网 时间:2024/06/08 09:09
普通文件与二进制文件间的相互转换
普通文件后缀为 ".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);//二进制转文本文件}}elseBinToNorfile(pzFilename);return 0;}
阅读全文
0 0
- 普通文件格式与二进制文件格式互转
- [DirectShow] 文件格式与压缩格式
- DirectShow 文件格式与压缩格式
- 图片与二进制文件的互转
- docx文件格式转PDF格式
- [DirectShow] 文件格式与压缩格式 转贴
- requery 与普通js 格式区别
- 【转】文本文件与二进制文件区别
- .obj文件格式与.mtl文件格式(转)
- 二进制文件与16进制(十六进制)文本文件互转工具
- 关于二进制文件储存格式
- 关于二进制文件储存格式
- 二进制文件格式装载支持
- 二进制文件格式设计
- IDEA-->JAVA 一般文件格式转PDF格式
- Dos文件格式与Unix格式转换:dos2unix,fromdos,todos
- Base64 格式的多个图片 转 普通路径格式
- 文本文件与二进制文件的区别(转)
- as86汇编程序的编译与链接
- 【Java进阶】实现自己的ORM框架
- 进程 线程
- 微分享-快速高效的从一个list转化到另一个list
- Detecting Skin in Images & Video Using Python and OpenCV皮肤检测
- 普通文件格式与二进制文件格式互转
- 动态规划-最大子段和系列问题
- Java多线程编程之Active Object模式
- 关注民生民情
- 推荐: XtraBackup 不停机不锁表搭建MySQL主从同步实践
- iOS json解析 和生成json串
- 安卓环境搭建的需要材料链接
- Windows下Python3安装BeautifulSoup4显示'You are trying to run the Python 2 version of Beautiful Soup under
- 安卓SDK需要下载哪些内容