二进制文件的读取并保存为文本文件

bool cFileCtrl::parseBinFile()
{
 FILE * sm= NULL;
 if(NULL == (sm = fopen("packUI","w+r")))
 {
  CreateFile("packUI",0,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
  fwrite("",sizeof(char),0,sm);
 }
 fclose(sm);

 FILE * uifile = NULL;

 char workPath[256];
 GetCurrentDirectory( 256, workPath);
 sprintf( workPath, "%s%s", workPath, "//packUI");
 if( (uifile = fopen( workPath, "r+b" )) == NULL )
  printf( "The file packUI was not opened/n");
 else
 {
  fseek(uifile,0L,SEEK_END);  
 }

 ifstream binfile("packUI.bin",ios::binary);

 if (binfile.rdbuf() == NULL)
 {
  printf( "The file packUI.bin was not opened/n");
  return false;
 }
 else
 { 
  binfile.seekg(0,ios::end);//流结束地址值
  long length = binfile.tellg(); 
  char* allc = (char*)malloc(length+1);
  memset(allc, 0, length+1); 
  binfile.seekg(0,ios::beg);//流开始地址值

  int tt = 0;  
  char* p = allc;
  while(!binfile.eof())
  { 
   char buf[128];
   ZeroMemory( buf, 128);
   binfile.read(buf,128);
   tt = binfile.gcount();
   memcpy(p, buf, tt);
   p += tt;
  }
  fwrite( allc, sizeof(byte), length, uifile);
  free(allc);
 }
 fclose(uifile);

 return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值