闲云控制台(二)查看文件功能,支持十六进制查看文件

命令处理区增加

int Look_File(pCommand b)
{
	int _out_style = 1;
	int _line_number = 1;
	int _len_argu = strlen(b->Cmd_Arguments);
	while (--_len_argu >= 0)
	{
		switch (b->Cmd_Arguments[_len_argu])
		{
		case 'x':
			_out_style = 16;
			break;
		case 'n':
			_line_number = 0;
			break;
		}
	}
	FILE *in_file = fopen(b->Cmd_Object, "rb");
	if (!in_file)
	{
		printf("文件不存在或路径错误。
");
		return NO;
	}
	else
	{
		unsigned char ch;
		int line = 1;
		int n = 0;
		if (_line_number) printf("%04d: ", line);
		while (EOF != fscanf(in_file, "%c", &ch))
		{
			if (16 == _out_style)
			{
				printf("%0X%0X", ch / 16, ch % 16);
				if (!(n = (n+1)%16)) 
					printf("
");
				else 
					printf(" ");
				if (_line_number && !n)
					printf("%04d: ", ++line);
			}
			else
			{
				printf("%c", ch);
				if (_line_number && '
' == ch)
					printf("%04d: ", ++line);
			}
		}
	}
	fclose(in_file);
	printf("
");
	return OK;
}


注冊区增加

_register_one_cmd("look", Look_File, "查看文件, 參数:x十六进制,n取消行号");


原文地址:https://www.cnblogs.com/mengfanrong/p/5197283.html