文件查看器(C语言)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LNZ001/article/details/54881512

直接,代码如下:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAXLEN 100

const int DISPLAY = 80;
const int PAGE_LENGTH = 20;

int main(int argc,char *argv[]){

	char filename[MAXLEN];
	FILE *pfile = NULL;

	unsigned char buffer[DISPLAY/4-1];
	int count = 0;
	int lines = 0;

	//要求输入文件名
	if(argc == 1){
		printf("请输入文件名:\n");//该情况需要sudo权限执行程序,才能访问其他文件夹文件.
		scanf("%s",filename);
		//printf("%s\n",filename);
	}else{
		strcpy(filename,argv[1]);
		printf("执行程序:%s\n",argv[0]);
	}
	

	//打开文件
	if(!(pfile = fopen(filename,"rb"))){
		printf("未能打开该文件.\n");
		return -1;
	}

	//读取并显示文件内容
	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);
	return 0;
}


猜你喜欢

转载自blog.csdn.net/LNZ001/article/details/54881512