buffer

Reply

Join Date: Nov 2004
Posts: 45
Reputation: jifiii is an unknown quantity at this point 
Solved Threads: 1
jifiii jifiii is offline Offline
Light Poster

buffer

 
0
  #1
Nov 5th, 2004
Here is my code I am trying to figure out what is wrong with it, can anybody help me.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int GetBuffer(char* buffer[]);
  6. int ParseArgs(int argc, char* argv[], int* canonical);
  7.  
  8. main(int argc, char* argv[])
  9. {
  10. int canonical=0; //If it is true, print the ascii with the hex
  11. int rc;
  12. char buffer[16]; //a character array to store 16 bytes at a time
  13.  
  14. int count;
  15. int offsetCount=0; //Keeps a count for the offset.
  16.  
  17. rc=ParseArgs(argc, argv, &canonical);
  18. //if rc returns 0, the arguments are good,
  19. //if it returns 1, there is a problem
  20.  
  21. if(rc!=0)
  22. {
  23. printf("Please use appropriate arguments\n view help screen if necessary)\n");
  24. return;
  25. }
  26.  
  27. do
  28. {
  29. count=GetBuffer( buffer);
  30. if (count>0)
  31. {
  32. printf("%08x ",offsetCount); //prints the offset of bytes into the file.
  33. offsetCount+=count; //Increments the offset based on the # of characters read.
  34. PrintHex(count, buffer); //Prints the hex values of the characters in the file.
  35. if (canonical)
  36. {
  37. PrintAscii(count, buffer);
  38. }
  39. }
  40. }
  41. while(count>0);
  42.  
  43.  
  44. }
  45.  
  46. //ParseArgs checks the arguments to make sure they are valid,
  47. // and returns a numerical value based on that check.
  48.  
  49. int ParseArgs(int argc, char* argv[], int* canonical)
  50. {
  51. switch(argc)
  52. {
  53. case 1:
  54. return 0;
  55. break;
  56. case 2:
  57. if (argv[1][0]=='-')
  58. {
  59. switch(argv[1][1])
  60. {
  61. case 'C':
  62. if (!argv[1][2])
  63. {
  64. *canonical=1;
  65. return 0;
  66. }
  67. else
  68. return 1;
  69. break;
  70. case 'H':
  71. if (!argv[1][2])
  72. {
  73. return 0;
  74. }
  75. else
  76. return 1;
  77. break;
  78. default:
  79. return 1;
  80. }
  81. }
  82. break;
  83. default:
  84. return 1;
  85. break;
  86. }
  87. }
  88.  
  89.  
  90. int GetBuffer(char* buffer[])
  91. {
  92. char tempchar; //Temporarily stores character from standard in,
  93. //to check for EOF.
  94. int counter=0; //Keeps a count of how many bytes are stored in buffer.
  95.  
  96. while(((tempchar=getchar())!=EOF)&&(counter < 16))
  97. {
  98. *buffer[counter]=tempchar;
  99. counter++;
  100. }
  101. return counter;
  102. }

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.
Last edited by alc6379; Nov 7th, 2004 at 5:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: buffer

 
0
  #2
Nov 6th, 2004
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:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int GetBuffer(char buffer[]);
  6. int ParseArgs(int argc, char* argv[], int* canonical);
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10. int canonical=0; /*If it is true, print the ascii with the hex*/
  11. int rc;
  12. char buffer[16];
  13.  
  14. int count;
  15. int offsetCount=0;
  16.  
  17. rc=ParseArgs(argc, argv, &canonical);
  18. /*if rc returns 0, the arguments are good,
  19.   if it returns 1, there is a problem*/
  20.  
  21. if(rc!=0)
  22. {
  23. printf("Please use appropriate arguments\n view help screen if necessary)\n");
  24. return EXIT_FAILURE;
  25. }
  26.  
  27. do
  28. {
  29. count=GetBuffer( buffer);
  30. if (count>0)
  31. {
  32. printf("%08x ",offsetCount);
  33. offsetCount+=count;
  34. /*PrintHex(count, buffer);*/
  35. if (canonical)
  36. {
  37. /*PrintAscii(count, buffer);*/
  38. }
  39. }
  40. }
  41. while(count>0);
  42.  
  43. return EXIT_SUCCESS;
  44. }
  45.  
  46. /*ParseArgs checks the arguments to make sure they are valid,
  47.   and returns a numerical value based on that check.*/
  48.  
  49. int ParseArgs(int argc, char* argv[], int* canonical)
  50. {
  51. switch(argc)
  52. {
  53. case 1:
  54. return 0;
  55. case 2:
  56. if (argv[1][0]=='-')
  57. {
  58. switch(argv[1][1])
  59. {
  60. case 'C':
  61. if (!argv[1][2])
  62. {
  63. *canonical=1;
  64. return 0;
  65. }
  66. break;
  67. case 'H':
  68. if (!argv[1][2])
  69. {
  70. return 0;
  71. }
  72. break;
  73. }
  74. }
  75. break;
  76. }
  77.  
  78. return 1;
  79. }
  80.  
  81.  
  82. int GetBuffer(char buffer[])
  83. {
  84. int tempchar;
  85. int counter=0;
  86.  
  87. while(((tempchar=getchar())!=EOF)&&(counter < 16))
  88. {
  89. buffer[counter]=(char)tempchar;
  90. counter++;
  91. }
  92.  
  93. return counter;
  94. }
Any logical errors are up to you because you neglected to finish the program. This makes testing more difficult.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 45
Reputation: jifiii is an unknown quantity at this point 
Solved Threads: 1
jifiii jifiii is offline Offline
Light Poster

Re: buffer

 
0
  #3
Nov 6th, 2004
Ok, here is my whole code, I finished it last night, but I am still getting errors.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void PrintHex (int count, char* buffer[]);
  6. void PrintAscii (int count, char* buffer[]);
  7. int getbuffer(char* buffer[]);
  8. int ParseArgs(int argc, char* argv[], int* canonical);
  9.  
  10. main(int argc, char* argv[])
  11. {
  12. int canonical=0; //If it is true, print the ascii with the hex
  13. int rc;
  14. char buffer[16]; //a character array to store 16 bytes at a time
  15.  
  16. int count;
  17. int offsetCount=0; //Keeps a count for the offset.
  18.  
  19. rc=ParseArgs(argc, argv, &canonical);
  20. //if rc returns 0, the arguments are good,
  21. //if it returns 1, there is a problem
  22.  
  23. if(rc!=0)
  24. {
  25. printf("Please use appropriate arguments\n view help screen if necessary)\n");
  26. return;
  27. }
  28.  
  29. do
  30. {
  31. count=GetBuffer( buffer);
  32. if (count>0)
  33. {
  34. printf("%08x ",offsetCount); //prints the offset of bytes into the file.
  35. offsetCount+=count; //Increments the offset based on the # of characters read.
  36. PrintHex(count, buffer[16]); //Prints the hex values of the characters in the file.
  37. if (canonical)
  38. {
  39. PrintAscii(count, buffer);
  40. }
  41. }
  42. }
  43. while(count>0);
  44.  
  45.  
  46. }
  47.  
  48. //ParseArgs checks the arguments to make sure they are valid,
  49. // and returns a numerical value based on that check.
  50.  
  51. int ParseArgs( int argc, char *argv[], int *canonical )
  52. {
  53. int rc;
  54. switch (argc)
  55. {
  56. case 0://argc = 0, not likely but cover anyway.
  57. {
  58. rc = 1;// error return code
  59. break;
  60. }
  61. case 1:// argc = 1, the program was invoked with no arguments
  62. {
  63. canonical = 0;// set this to zero to take default of no Hex
  64. rc = 0;// ok return code
  65. }
  66. case 2:// argc = 2, the program was invoked with arguments
  67. {
  68. if ( argv[1][0] == '-' && argv[1][1] == 'C')//is -C on the command line
  69. {
  70. canonical = 1;
  71. rc = 0;
  72. }
  73. else
  74. {
  75. canonical = 0;
  76. rc = 0;
  77. }
  78. }
  79. }
  80. }
  81.  
  82.  
  83. int getbuffer( char* buffer )
  84. {
  85. int c;
  86. int i;
  87. //int i = 0;
  88. while ( ( c = getchar () ) != EOF )
  89. {
  90. for (i = 0; i <= 16; i++)//change this to something that will work!
  91. {
  92. buffer[i] = c;
  93. printf("%02x", buffer[i]);
  94. //i++;
  95. printf("%d", i);
  96. }
  97. }
  98. //printf("%d", i);
  99. return i;
  100. }
  101.  
  102.  
  103. void PrintHex(int count, char* buffer[])
  104. {
  105. int hexcount;
  106. for(hexcount=0;hexcount<8;hexcount++)
  107. {
  108. printf("%02x ",*buffer);
  109. }
  110. for(hexcount=0; hexcount<16; hexcount++);
  111. }
  112.  
  113. void PrintAscii (int count, char* buffer[])
  114. {
  115. int asciicount;
  116. for(asciicount=0;asciicount<8;asciicount++)
  117. {
  118. printf("%02x ", *buffer);
  119. }
  120. for(asciicount=0; asciicount<16; asciicount++);
  121. }

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.
Last edited by alc6379; Nov 7th, 2004 at 5:39 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: buffer

 
0
  #4
Nov 6th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 45
Reputation: jifiii is an unknown quantity at this point 
Solved Threads: 1
jifiii jifiii is offline Offline
Light Poster

Re: buffer

 
0
  #5
Nov 6th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: buffer

 
0
  #6
Nov 6th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC