69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <conio.h>
FILE *fi,*fo;
unsigned int n;
int c;
//-------------------------------------------------------
void main(int argc,char *argv[])
{
if (argc<3) {
cprintf("Bin2Hex srcfile desfile");
return;
}
if ((fi=fopen(argv[1],"rb"))==NULL) {
cprintf("Can not find file %s",argv[1]);
return;
}
if ((fo=fopen(argv[2],"w"))==NULL) {
fclose(fi);
cprintf("Can not create file %s",argv[2]);
return;
}
n=0;
while (1) {
c=fgetc(fi);
if (EOF==c) break;
n++;
if (1==n) fprintf(fo, "0x%02X",c);
else {
if (1==n%16) fprintf(fo,"\n0x%02X",c);
else fprintf(fo, ",0x%02X",c);
}
}
fcloseall();
cprintf("OK to Bin2Hex %u bytes.",n);
}
fp = fopen(fileName1, "rb+");
if (NULL == fp)
{
printf("Cannot open the %s ", fileName1);
}
else
{
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
if(fileSize > 0)
{
buf = malloc(fileSize+1);
if(NULL != buf)
{
memset(buf, 0, fileSize+1);
fread(buf, 1, fileSize, fp);
}
else
{
printf( "read file: failure to allocate memory");
}
}
fclose(fp);
if (NULL != buf)
{
fp = fopen(fileName2, "w");
if(NULL != fp)
{
fwrite(buf, 1, strlen(buf), fp);
fclose(fp);
}
free(buf);
}
else
{
printf("File is empty !");
}
}