J
jacob navia
Got the idea of adding one more exercise to the tutorial.
The goal is to show a small hexdump utility without any bells and
whistles, and add a bunch of exercises to add those. Here it is.
It uses standard C. Please tell me if there could be any
portability problems.
I do not use putchar but fputc to make it easier to add an output
file later as one more argument.
Please tell me if you see any errors in it. Note that the manifest
constants will be replaced by #defines in the exercises, when they
are asked to increase the number of columns, etc.
------------------------------------------------------cut here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
if (argc < 2) {
fprintf(stderr,"Usage: %s <file name>\n",argv[0]);
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1],"rb");
if (file == NULL) {
fprintf(stderr,"Impossible to open %s for reading\n",argv[1]);
return EXIT_FAILURE;
}
int oneChar = fgetc(file);
int column = 0,line = 0;
unsigned char tab[16+1];
char *hex = "0123456789abcdef";
int address = 1;
while (oneChar != EOF) {
if (column == 0) {
memset(tab,'.',16);
fprintf(stdout,"[%d] ",address);
}
if (oneChar >= ' ' && oneChar <= 127) {
tab[column] = oneChar;
}
fputc(hex[(oneChar >> 4)&0xf],stdout);
fputc(hex[oneChar&0xf],stdout);
fputc(' ',stdout);
column++;
if (column == 16) {
fputc(' ',stdout);
tab[column]=0;
fputs(tab,stdout);
column = 0;
}
line++;
if (line == 16) {
fputc('\n',stdout);
line = 0;
}
oneChar = fgetc(file);
address++;
}
fclose(file);
address--;
if (column > 0 ) {
while (column < 16) {
fprintf(stdout," ");
tab[column]=' ';
column++;
}
tab[16]=0;
fprintf(stdout," %s\n[%d]\n",tab,address);
}
else fprintf(stdout,"[%d]\n",address);
return EXIT_SUCCESS;
}
----------------------------------------------------cut here
The goal is to show a small hexdump utility without any bells and
whistles, and add a bunch of exercises to add those. Here it is.
It uses standard C. Please tell me if there could be any
portability problems.
I do not use putchar but fputc to make it easier to add an output
file later as one more argument.
Please tell me if you see any errors in it. Note that the manifest
constants will be replaced by #defines in the exercises, when they
are asked to increase the number of columns, etc.
------------------------------------------------------cut here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
if (argc < 2) {
fprintf(stderr,"Usage: %s <file name>\n",argv[0]);
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1],"rb");
if (file == NULL) {
fprintf(stderr,"Impossible to open %s for reading\n",argv[1]);
return EXIT_FAILURE;
}
int oneChar = fgetc(file);
int column = 0,line = 0;
unsigned char tab[16+1];
char *hex = "0123456789abcdef";
int address = 1;
while (oneChar != EOF) {
if (column == 0) {
memset(tab,'.',16);
fprintf(stdout,"[%d] ",address);
}
if (oneChar >= ' ' && oneChar <= 127) {
tab[column] = oneChar;
}
fputc(hex[(oneChar >> 4)&0xf],stdout);
fputc(hex[oneChar&0xf],stdout);
fputc(' ',stdout);
column++;
if (column == 16) {
fputc(' ',stdout);
tab[column]=0;
fputs(tab,stdout);
column = 0;
}
line++;
if (line == 16) {
fputc('\n',stdout);
line = 0;
}
oneChar = fgetc(file);
address++;
}
fclose(file);
address--;
if (column > 0 ) {
while (column < 16) {
fprintf(stdout," ");
tab[column]=' ';
column++;
}
tab[16]=0;
fprintf(stdout," %s\n[%d]\n",tab,address);
}
else fprintf(stdout,"[%d]\n",address);
return EXIT_SUCCESS;
}
----------------------------------------------------cut here