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.

Recommended Answers

All 5 Replies

When posting relatively long code compared to the tiny toy programs usually posted, it's best to include the exact error messages you get, and the comments on the lines that cause them if applicable. Here's your code with the requisite changes made to compile and link as C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int GetBuffer(char buffer[]);
int ParseArgs(int argc, char* argv[], int* canonical);

int main(int argc, char* argv[])
{
  int canonical=0; /*If it is true, print the ascii with the hex*/
  int rc; 
  char buffer[16];

  int count;
  int offsetCount=0;

  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 EXIT_FAILURE;
  }

  do
  {
    count=GetBuffer( buffer);
    if (count>0)
    {
      printf("%08x ",offsetCount);
      offsetCount+=count;
      /*PrintHex(count, buffer);*/
      if (canonical)
      {
        /*PrintAscii(count, buffer);*/
      }
    }
  }
  while(count>0);

  return EXIT_SUCCESS;
}

/*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;
  case 2:
    if (argv[1][0]=='-')
    {
      switch(argv[1][1])
      {
      case 'C': 
        if (!argv[1][2])
        {
          *canonical=1;
          return 0;
        }
        break;
      case 'H':
        if (!argv[1][2])
        { 
          return 0;
        }
        break;
      }
    }
    break;
  }

  return 1;
}


int GetBuffer(char buffer[])
{
  int tempchar;
  int counter=0;

  while(((tempchar=getchar())!=EOF)&&(counter < 16))
  { 
    buffer[counter]=(char)tempchar;
    counter++;
  }

  return counter;
}

Any logical errors are up to you because you neglected to finish the program. This makes testing more difficult.

Ok, here is my whole code, I finished it last night, but I am still getting errors.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void PrintHex (int count, char* buffer[]);
void PrintAscii (int count, char* buffer[]);
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[16]);      //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 )
{
  int rc;
  switch (argc)
    {
    case 0://argc = 0, not likely but cover anyway.
      {
	rc = 1;// error return code
	break;
      }
    case 1:// argc = 1, the program was invoked with no arguments
      {
	canonical = 0;// set this to zero to take default of no Hex
	rc = 0;// ok return code
      }
    case 2:// argc = 2, the program was invoked with arguments
      {
	if ( argv[1][0] == '-' && argv[1][1] == 'C')//is -C on the command line
	  {
	    canonical = 1;
	    rc = 0;
	  }
	else
	  {
	    canonical = 0;
	    rc = 0;
	  }
      }
    }
}


int getbuffer( char* buffer )
{
  int c;
  int i;
  //int i = 0;
  while ( ( c = getchar () ) != EOF )
    {
      for (i = 0; i <= 16; i++)//change this to something that will work!
	{
	  buffer[i] = c;
	  printf("%02x", buffer[i]);
	  //i++;
	  printf("%d", i);
	}
    }
  //printf("%d", i);
  return i;
}


void PrintHex(int count, char* buffer[])
{
  int hexcount;
  for(hexcount=0;hexcount<8;hexcount++)
    {
      printf("%02x ",*buffer);
    }
  for(hexcount=0; hexcount<16; hexcount++);
}

void PrintAscii (int count, char* buffer[])
{
  int asciicount;
  for(asciicount=0;asciicount<8;asciicount++)
    {
      printf("%02x ", *buffer);
    }
  for(asciicount=0; asciicount<16; asciicount++);
}

But my errors for this code are
line (37) "warning C4047: 'function' : 'char** ' differs in levels of indirection from 'char'"
line (40) "warning C4047: 'function' : 'char** ' differs in levels of indirection from 'char'
line(71) "warning c4047: '=' : 'int *' differs in levels on indirection from 'int'
line(81) "warning C4716: 'ParseArgs': must return a value
error LNK2019: unresolved external symbol _GetBuffer referenced in function_main
fatal error LNK 1120: 1 Unresolved externals

Any helpy would be appreciated.

>Any helpy would be appreciated.
No. You're making the same mistakes that I fixed, so figure out what changes I made in your old code and do the same thing. And check your spelling.

Ok, what you ttold me to change only printed out the offset for the file not the hexadecimal or ASCII for what I needed, can you look over the code again, and please tell me how to print out the HEX or ASCII when asked too.

>what you ttold me to change only
What I told you fixed the problems you had compiling and linking. The problems you had were with an incomplete program. It's not my job to do your homework for you, so I didn't write those functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.