Question
I have a problem to solve in my computer science class: You shall develop, test, and deliver a simple hexdump program, xbd. The simple hexdump
I have a problem to solve in my computer science class: "You shall develop, test, and deliver a simple hexdump program, xbd. The simple hexdump program will open a regular type file (binary or text/ASCII, read every byte in the file and write both the ASCII hex value for that byte as well as its printable (human-readable) character (characters, digits, symbols) to standard output. For bytes forming non-printable characters, print a . character (a dot/period character, i.e. hex value 2E)." So far I have wrote this code (written in C):
--------------------------------------------------------------------------------------------------------------------------------------------
#include
#define SIZE 1000
void hexDump (void *addr, int len) {
int i;
unsigned char buff[19];
unsigned char *pc = (unsigned char*)addr;
for (i = 0; i < len; i++) {
if ((i % 16) == 0) {
if (i != 0)
printf(" %s ", buff);
printf(" %07x: ", i); //prints address
}
printf ("%02x", pc[i]);
if ((i % 2) == 1)
printf(" ");
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) //non-printable chars
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0'; //clear array
}
while ((i % 16) != 0) { //'.' fill last line
printf (" ");
i++;
}
printf (" %s ", buff);
}
int main (int argc, char *argv[]) {
FILE *fp;
char buff[SIZE];
fp = fopen(argv[1], "r");
memset(buff, '\0', sizeof(buff));
fgets(buff, sizeof(fp), fp);
hexDump(&buff, sizeof(fp);
fclose(fp);
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- The main problem I am having is that fgets stops at the so its only reads the first line of a file.
I tried using a while loop instead but then it resets the memory address each time.
The last printout of each line is always 00 for some reason. Other than that, the progam runs fine, what can I do to fix this specific issue?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started