您的位置:首页 > 其它

C 二进制读取文件

2015-08-27 11:26 309 查看
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXLEN 256
#define DISPLAY 80
#define PAGE_LENGTH 20

int main()
{

char filename[MAXLEN]="d:\\备忘.txt";
FILE *pfile = NULL;
unsigned char buffer[DISPLAY/4-1];
int count = 0;
int lines = 0;

/*
puts("输入文件路径:");
fgets(path,256,stdin);

int len = strlen(path);
if(path[len-1]=='\n')
path[len-1]='\0';
*/

pfile = fopen(filename,"rb");

while(!feof(pfile))
{
if(count< sizeof buffer)
buffer[count++] = (unsigned char)fgetc(pfile);
else
{
for(count =0 ;count < sizeof buffer; count++)
printf("%02X ",buffer[count]);
printf("| ");

for(count =0 ;count < sizeof buffer; count++)
printf("%c",isprint(buffer[count])?buffer[count]:'.');
printf("\n");
count = 0;

if(!(++lines%PAGE_LENGTH))
if(getchar()=='E')
return 0;

}
}

for(int i=0;i< sizeof buffer;i++)
if(i<count)
printf("%02X ",buffer[i]);
else
printf("   ");
printf("| ");
for(int i=0;i< count;i++)
printf("%c",isprint(buffer[i])?buffer[i]:'.');
printf("\n");
fclose(pfile);
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C