第323页 15. 输入/输出函数
- 章节名:15. 输入/输出函数
- 页码:第323页
习题.编写一个程序,产生一个文件的十六进制倾印码(dump)……格式要求: 列1-6 文件的当前偏移位置,用十六进制表示,前面用零填充 列9-43 文件接下来的16个字节的十六进制表示形式。它们分成四组,每组由8个十六进制数字组成,每组之前以一个空格间隔 列46 一个星号 列47-62 文件中上述16个字节的字符表示形式。如果某个字符不可打印或空白,就以一个句点表示 列63 一个星号
#include <stdio.h> #include <string.h> #define col_size 16 #define rep_hex_size 36 //丑陋的代码T T void hex_line(int pos, int col, int *data, char *hex){ char buffer[3]; int hex_pos = 0; int end = pos % col_size == col ? col :col_size; //判断最后一行 for(int i=0;i<end; i++){ if(i%4 == 0 && i >0){ hex[hex_pos++] = ' '; } sprintf(buffer, "%X", data[i]); //16进制表示 hex[hex_pos++] =buffer[0]; hex[hex_pos++] =buffer[1]; } while(hex_pos < rep_hex_size-1){ hex[hex_pos++] = (hex_pos+1)%9 == 0 ? ' ' : '0'; //以0填充 } hex[rep_hex_size-1]='\0'; } //读取文件,dump输出 void dump_output(char *file){ FILE *input = NULL; int data[col_size]; char rep_hex[rep_hex_size]; char rep_char[col_size + 1]; int pos = 0; int col = pos % col_size; input = fopen(file, "r"); if(input != NULL){ while((data[col] = fgetc(input))!= EOF){ if(data[col] == '\r' || data[col] == '\n') continue; rep_char[col] = isprint(data[col]) > 0 ? (char)data[col] : '.'; pos ++; if(pos % col_size == 0 && pos > 1){ hex_line(pos, col, data, rep_hex); rep_char[col_size] = '\0'; printf("%06X %s *%s*\n", pos - col_size, rep_hex, rep_char); } col = pos % col_size; } if(col > 0){ rep_char[col] = '\0'; hex_line(pos, col, data, rep_hex); printf("%06X %s *%s*\n", pos - pos%col_size , rep_hex, rep_char); } if(fclose(input) != 0){ perror(file); } } } int main(int argc, char **argv){ int exit_status = 0; if(*++argv != NULL){ dump_output(*argv); } return exit_status; }
44人阅读
龙三对本书的所有笔记 · · · · · ·
-
第171页 8.数组
题. 如果A是个x行y列的矩阵,B是个y行z列的矩阵。把A和B相乘,其结果将是另一个x行z列的矩阵C...
-
第276页 13.高级指针话题
编写一个名叫sort的函数,它用于对一个任何类型的数组进行排序...blabla.. #include <stri...
-
第323页 15. 输入/输出函数
-
第354页 16.13 习题10
插入排序(insertion sort)就是逐个把值插入到一个数组中。第1个值存储于数组的起始位置。每...
-
第386页 17.9 习题6
编写一个函数,检查一棵树是不是二叉搜索树。你可以选择任何一种你喜欢的树实现形式。 二叉搜...
> 查看全部9篇
说明 · · · · · ·
表示其中内容是对原文的摘抄