Here is my code I am trying to figure out what is wrong with it, can anybody help me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int GetBuffer(char* buffer[]);
int ParseArgs(int argc, char* argv[], int* canonical);
main(int argc, char* argv[])
{
int canonical=0; //If it is true, print the ascii with the hex
int rc;
char buffer[16]; //a character array to store 16 bytes at a time
int count;
int offsetCount=0; //Keeps a count for the offset.
rc=ParseArgs(argc, argv, &canonical);
//if rc returns 0, the arguments are good,
//if it returns 1, there is a problem
if(rc!=0)
{
printf("Please use appropriate arguments\n view help screen if necessary)\n");
return;
}
do
{
count=GetBuffer( buffer);
if (count>0)
{
printf("%08x ",offsetCount); //prints the offset of bytes into the file.
offsetCount+=count; //Increments the offset based on the # of characters read.
PrintHex(count, buffer); //Prints the hex values of the characters in the file.
if (canonical)
{
PrintAscii(count, buffer);
}
}
}
while(count>0);
}
//ParseArgs checks the arguments to make sure they are valid,
// and returns a numerical value based on that check.
int ParseArgs(int argc, char* argv[], int* canonical)
{
switch(argc)
{
case 1:
return 0;
break;
case 2:
if (argv[1][0]=='-')
{
switch(argv[1][1])
{
case 'C':
if (!argv[1][2])
{
*canonical=1;
return 0;
}
else
return 1;
break;
case 'H':
if (!argv[1][2])
{
return 0;
}
else
return 1;
break;
default:
return 1;
}
}
break;
default:
return 1;
break;
}
}
int GetBuffer(char* buffer[])
{
char tempchar; //Temporarily stores character from standard in,
//to check for EOF.
int counter=0; //Keeps a count of how many bytes are stored in buffer.
while(((tempchar=getchar())!=EOF)&&(counter < 16))
{
*buffer[counter]=tempchar;
counter++;
}
return counter;
}
This is what the program needs to do.
Command Line Arguements:
You will support two mutually exclusive command line arguement: -H and -C
-C displays the data in "canonical" Form( that is Hex, and ASCII output).
-H displays the data in hex only( This is the program default.)
Unlike the actual hexdump command, you will no support a named input file
You will read the input bytes stream from standdard in, and only form standard in.
If illegal command line arguements are supplied, print a meaningful helpscreen and then exit.
Output Format:
For the -C option, your output should look like the examples already given. (I will shwo the example at the bottom of the page)
For the -H option, your output should look like -C, only missing the ASCII display on the right.
If no command line option are given, then defaults to the -H display format.
Program Structure:
Reading standard input:
Create a 16 byte input buffer.
Read 16 bytes from standard input and store them in your input buffer.
You must handle two special cases
On yoru last buffer read, you will probaly get less than 16 bytes.
You must handle EOF
Formatting the hex output:
Create a function with the following prototypes: void printHex(int count, char buffer);
The "count" parameter is the number of bytes to format and output.
Cout will typically be 16, except for possible partial line at the end of the file.
Don't forget to protect against the special case of count==0
The "buffer parameter is a pointer to the data you will format.
The printhex() function should print "count" bytes to standard out, as in the exampes above:
Spaces between each byte.
Two hex digits per byte, even if the byte is small.
(Like FF 0E 01 not FF E 1)
One the last call, you may need ot print some spaces for hte missing bytes.
Formatting the ASCII output:
Create a function with the following prototype: void printASCII(int count, char buffer);
The "count" parameter is the number of bytes to format ad output.
The "buffer" parameter is a pointer to the dat you will format.
The printASCII() function should print "count" bytes to standard out, as in the examples above:
Make it look like the hexdump output, that is...
A vertical bat "|" to mark the left side of the ASCII data display.
Up to sixteen ASCII character, as in the examples above.
For unprintable bytes, use period (".")
A vertical bar "|" to mark the right side if the ASCII data display.