普通文件格式与二进制文件格式互转

普通文件与二进制文件间的相互转换

普通文件后缀为 ".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; } 
             
            
           
          
         
        
      
     
     
    
    
   
   
  
  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值