Today, I saw a program to display file content in the hexadecimal format. I ran a script on the machine and found that the hexadecimal format was not fully displayed. After GDB debugging, I found that the sprintf format was not correct, A space-_-is missing in the book -_-!
To put it bluntly, go to the Code:
/* <Br/> * = ========================================================== =============< br/> * <br/> * filename: myoD. c <br/> * Description: OD <br/> * version: 1.0 <br/> * created: 06/24/2011 02:52:00 <br/> * revision: None <br/> * Compiler: GCC <br/> * Author: Your name (), <br/> * company: <br/> * = ====================================== =================================< Br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> int main (INT argc, char * argv []) <br/>{< br/> int N, C; <br/> file * FP; <br/> char Buf [101], hexbuf [101]; <br/> If (argc! = 2) <br/>{< br/> fprintf (stderr, "Usage: % s filename/N", argv [0]); <br/> exit (0); <br/>}< br/> If (FP = fopen (argv [1], "R") = NULL) <br/>{< br/> perror ("fopen"); <br/> exit (0); <br/>}< br/> n = 0; <br/> memset (BUF, 0x00, sizeof (BUF); <br/> memset (hexbuf, 0x00, sizeof (hexbuf )); <br/> while (C = fgetc (FP ))! = EOF) <br/>{< br/> If (n> 15) <br/>{< br/> fprintf (stderr, "%-48 S % s/n", hexbuf, Buf); <br/> n = 0; <br/> memset (BUF, 0x00, sizeof (BUF); <br/> memset (hexbuf, 0x00, sizeof (hexbuf )); <br/>}< br/> Buf [N] = (unsigned char) C; <br/> sprintf (hexbuf + 3 * n, "% 02x", C ); <br/> N ++; <br/>}< br/> fprintf (stderr, "%-48 S % s/n", hexbuf, Buf ); <br/> fclose (FP); <br/> return 0; <br/>}
Code running result:
[Root @ localhost linuxc] #. /MyoD. /stdout. redirect <br/> 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6D this is a test m <br/> 65 73 73 61 67 65 2E 0a essage.