home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / BINDUMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-31  |  1.6 KB  |  48 lines

  1. /* BINDUMP.C - From page 446 of "Microsoft C Programming for    */
  2. /* the IBM" by Robert Lafore. Performs a binary dump of a file. */
  3. /* In the book there is a semicolon that doesn't belong at the  */
  4. /* end of the second if statement. Also there is a space after  */
  5. /* the ">" character in the first printf statement that doesn't */
  6. /* belong.                                                      */
  7. /****************************************************************/
  8.  
  9. #include <stdio.h>
  10. #define LENGTH 10 
  11. #define TRUE 0
  12. #define FALSE -1
  13.  
  14. main(argc, argv)
  15. int argc;
  16. char *argv[];
  17. {
  18. FILE *fileptr;
  19. unsigned int ch;
  20. int j, not_eof;
  21. unsigned char string[LENGTH + 1];
  22.  
  23.    if(argc != 2) {
  24.       printf("\nFormat: C>bindump filename");
  25.       exit();
  26.    }
  27.    if((fileptr = fopen(argv[1], "rb")) == NULL) {
  28.       printf("\nCan't open file %s.", argv[1]);
  29.       exit();
  30.    }
  31.    not_eof = TRUE;                  /*not EOF flag*/
  32.    do {
  33.       for(j = 0; j < LENGTH; j++) {
  34.          if((ch = getc(fileptr)) == EOF)     /*read character*/
  35.             not_eof = FALSE;              /*clear flag on EOF*/
  36.          printf("%3x ", ch);              /*print ASCII code*/
  37.          if(ch > 31)
  38.             *(string + j) = ch;           /*save printable char*/
  39.          else                             /*use period for*/
  40.             *(string + j) = '.';          /*nonprintable char*/
  41.       }
  42.       *(string + j) = '\0';               /*end string*/
  43.       printf("   %s\n", string);          /*print string*/
  44.    } while(not_eof == TRUE);              /*quit on EOF*/
  45.    fclose(fileptr);
  46. }
  47.  
  48.