home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / hexdump1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-01  |  2.6 KB  |  93 lines

  1. /* HEXDUMP.C illustrates directory splitting and character stream I/O.
  2.  * Functions illustrated include:
  3.  *      _splitpath      _makepath
  4.  *      fgetc           fputc           fgetchar        fputchar
  5.  *      getc            putc            getchar         putchar
  6.  *
  7.  * While fgetchar, getchar, fputchar, and putchar are not specifically
  8.  * used in the example, they are equivalent to using fgetc or getc with
  9.  * stdin, or to using fputc or putc with stdout. See FUNGET.C for another
  10.  * example of fgetc and getc.
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <conio.h>
  15. #include <io.h>
  16. #include <dos.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. main()
  21. {
  22.     FILE *infile, *outfile;
  23.     char inpath[_MAX_PATH], outpath[_MAX_PATH];
  24.     char drive[_MAX_DRIVE], dir[_MAX_DIR];
  25.     char fname[_MAX_FNAME], ext[_MAX_EXT];
  26.     int  in, size;
  27.     long i = 0L;
  28.  
  29.     /* Query for and open input file. */
  30.     printf( "Enter input file name: " );
  31.     gets( inpath );
  32.     strcpy( outpath, inpath );
  33.     if( (infile = fopen( inpath, "rb" )) == NULL )
  34.     {
  35.         printf( "Can't open input file" );
  36.         exit( 1 );
  37.     }
  38.  
  39.     /* Build output file by splitting path and rebuilding with
  40.      * new extension.
  41.      */
  42.     _splitpath( outpath, drive, dir, fname, ext );
  43.     strcpy( ext, "hx" );
  44.     _makepath( outpath, drive, dir, fname, ext );
  45.  
  46.     /* If file does not exist, open it */
  47.     if( access( outpath, 0 ) )
  48.     {
  49.         outfile = fopen( outpath, "wb" );
  50.         printf( "Creating %s from %s . . .\n", outpath, inpath );
  51.     }
  52.     else
  53.     {
  54.         printf( "Output file already exists" );
  55.         exit( 1 );
  56.     }
  57.  
  58.     printf( "(B)yte or (W)ord: " );
  59.     size = getche();
  60.  
  61.     /* Get each character from input and write to output. */
  62.     while( !feof( infile ) )
  63.     {
  64.         if( (size == 'W') || (size == 'w') )
  65.         {
  66.             in = getw( infile );
  67.             fprintf( outfile, " %.4X", in );
  68.             if( !(++i % 8) )
  69.                 putw( 0x0D0A, outfile );        /* New line      */
  70.         }
  71.         else
  72.         {
  73.             /* This example uses the fgetc and fputc functions. You
  74.              * could also use the macro versions:
  75.             in = getc( infile );
  76.              */
  77.             in = fgetc( infile );
  78.             fprintf( outfile, " %.2X", in );
  79.             if( !(++i % 16) )
  80.             {
  81.                 /* Macro version:
  82.                 putc( 13, outfile );
  83.                  */
  84.                 fputc( 13, outfile );           /* New line      */
  85.                 fputc( 10, outfile );
  86.             }
  87.         }
  88.     }
  89.     fclose( infile );
  90.     fclose( outfile );
  91.     exit( 0 );
  92. }
  93.