C++读写二进制文件

C++读写二进制文件C++读写二进制文件代码#include"stdafx.h"

#include
"stdio.h"

#include
"string.h"

#include
"stdlib.h"

#defineMAXLEN 1024

voidFill0(chart[])

{



if(strlen(t)<2)

{

t[
1]=t[0];

t[
0]='0';

t[
2]=0;

}

}

intbinaryToString(charfrom,charto)

{

FILE
fp;

FILE
out;



fp
=fopen(from,"rb");

out=fopen(to,"w");

if(!fp||!out){

printf(
"error!n");

return-1;

}

unsigned
charc;

intcount;



charcc[2];

intsize=0;

while((count=fread(&c,1,1, fp))==1)

{





sprintf(cc,
"%x",c);

Fill0(cc);





fwrite(cc,
1,2,out);



size
++;



}

printf(
"total:%d",size);

fclose(
out);

fclose(fp);

return0;

}

intstringToBinary(charfrom,charto)

{

FILE
fp;

FILE
out;



fp
=fopen(from,"r");

out=fopen(to,"wb");

if(!fp||!out){

printf(
"error!n");

return-1;

}



intcount;



charcc[2];

intsize=0;

intd=0;

while((count=fread(cc,1,2, fp))==2)

{



sscanf(cc,
"%2x",&d);





fwrite(
&d,1,1,out);



size
++;



}

printf(
"total:%d",size);

fclose(
out);

fclose(fp);

return0;

}





intmain(intargc,char*argv[])

{



if(argc!=4)

{

printf(
"cmd -tos|tob n");

getchar();

return-1;

}

if(strcmp(argv[1],"tos")==0)

{

binaryToString(argv[
2],argv[3]);

}

elseif(strcmp(argv[1],"tob")==0)

{

stringToBinary(argv[
2],argv[3]);

}

return1;

}

原文链接: https://www.cnblogs.com/yangyh/archive/2010/05/20/1740371.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/11030

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 上午12:40
下一篇 2023年2月7日 上午12:40

相关推荐