展开全部
#include <stdio.h> //标准输入输入出的头文件,printf和scanf都在这里了
#include <stdlib.h> //标准库头文件,内存分配、数学运算等都在这里了
#define MAXLEN 1024
int main(int argc, char *argv[])
{
if( argc < 3 )
{
printf("usage: %s %s/n", argv[0], "infile outfile");
exit(1);
}
FILE * outfile, *infile;
outfile = fopen(argv[2], "wb" );
infile = fopen(argv[1], "rb");
unsigned char buf[MAXLEN];
if( outfile == NULL || infile == NULL )
{
printf("%s, %s",argv[1],"not exit/n");
exit(1);
}
int rc;
while( (rc = fread(buf,sizeof(unsigned char),MAXLEN,infile)) != 0 )
{
fwrite( buf, sizeof( unsigned char ), rc, outfile );
}
fclose(infile);
fclose(outfile);
return 0;
}
#include <stdlib.h> //标准库头文件,内存分配、数学运算等都在这里了
#define MAXLEN 1024
int main(int argc, char *argv[])
{
if( argc < 3 )
{
printf("usage: %s %s/n", argv[0], "infile outfile");
exit(1);
}
FILE * outfile, *infile;
outfile = fopen(argv[2], "wb" );
infile = fopen(argv[1], "rb");
unsigned char buf[MAXLEN];
if( outfile == NULL || infile == NULL )
{
printf("%s, %s",argv[1],"not exit/n");
exit(1);
}
int rc;
while( (rc = fread(buf,sizeof(unsigned char),MAXLEN,infile)) != 0 )
{
fwrite( buf, sizeof( unsigned char ), rc, outfile );
}
fclose(infile);
fclose(outfile);
return 0;
}
追问
输入到文件中还是显示乱码,我想的是把那些16进制数字输入文件中,可以做到吗?
展开全部
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE * fp;
char * pbuf;
long fsize = 0;
int i = 0;
int newline = 0;
//printf("argc = %d, filename = %s\n", argc, argv[1]);
if(argc != 2)
{
printf("usage: ./dump filename\n");
return 1;
}
fp = fopen(argv[1], "rb");
if(!fp)
{
perror("error ");
return 2;
}
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
pbuf = (char *)malloc(fsize);
if(NULL == pbuf)
{
printf("malloc failed!\n");
return 3;
}
fread(pbuf, 1, fsize, fp);
while(i < fsize)
{
printf("%3x", pbuf[i]);
i++;
if(++newline == 16)
{
printf("\n");
newline = 0;
}
}
printf("\n");
free(pbuf);
fclose(fp);
return 0;
}
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE * fp;
char * pbuf;
long fsize = 0;
int i = 0;
int newline = 0;
//printf("argc = %d, filename = %s\n", argc, argv[1]);
if(argc != 2)
{
printf("usage: ./dump filename\n");
return 1;
}
fp = fopen(argv[1], "rb");
if(!fp)
{
perror("error ");
return 2;
}
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
pbuf = (char *)malloc(fsize);
if(NULL == pbuf)
{
printf("malloc failed!\n");
return 3;
}
fread(pbuf, 1, fsize, fp);
while(i < fsize)
{
printf("%3x", pbuf[i]);
i++;
if(++newline == 16)
{
printf("\n");
newline = 0;
}
}
printf("\n");
free(pbuf);
fclose(fp);
return 0;
}
更多追问追答
追问
为什么有的数会显示一堆ffffffff,请问要怎么处理。
追答
把第7行, char * pbuf的定义改为 unsigned char * pbuf就可以了
另外, 把第39行 修改为 printf("%3.2x", pbuf[i]);
输出会更美观一些
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |