Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
HEXDUMP.C
◄Up► ◄Contents► ◄Index► ◄Back►
────────────────────────────────────────────────────────────────────────────
/* HEXDUMP.C illustrates directory splitting and character stream I/O.
* Functions illustrated include:
* _splitpath _makepath getw putw
* fgetc fputc fgetchar fputchar
* getc putc getchar putchar
* _fsopen
*
* While fgetchar, getchar, fputchar, and putchar are not specifically
* used in the example, they are equivalent to using fgetc or getc with
* stdin, or to using fputc or putc with stdout. See FUNGET.C for another
* example of fgetc and getc.
*/
#include <stdio.h>
#include <conio.h>
#include <share.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void main()
{
FILE *infile, *outfile;
char inpath[_MAX_PATH], outpath[_MAX_PATH];
char drive[_MAX_DRIVE], dir[_MAX_DIR];
char fname[_MAX_FNAME], ext[_MAX_EXT];
int in, size;
long i = 0L;
/* Query for and open input file. */
printf( "Enter input file name: " );
gets( inpath );
strcpy( outpath, inpath );
if( (infile = fopen( inpath, "rb" )) == NULL )
{
printf( "Can't open input file: %d", errno );
exit( 1 );
}
/* Build output file by splitting path and rebuilding with
* new extension.
*/
_splitpath( outpath, drive, dir, fname, ext );
strcpy( ext, "hx" );
_makepath( outpath, drive, dir, fname, ext );
/* Open output file for writing. Using _fsopen allows use to ensure
* that no one else writes to the file while we are writing to it.
*/
if( (outfile = _fsopen( outpath, "wb", SH_DENYWR )) == NULL )
{
printf( "Can't open output file: %d", errno );
exit( 1 );
}
printf( "Creating %s from %s . . .\n", outpath, inpath );
printf( "(B)yte or (W)ord: " );
size = getche();
/* Get each character from input and write to output. */
while( 1 )
{
if( (size == 'W') || (size == 'w') )
{
in = getw( infile );
if( (in == EOF) && (feof( infile ) || ferror( infile )) )
break;
fprintf( outfile, " %.4X", in );
if( !(++i % 8) )
putw( 0x0A0D, outfile ); /* New line */
}
else
{
/* This example uses the fgetc and fputc functions. You
* could also use the macro versions:
in = getc( infile );
*/
in = fgetc( infile );
if( (in == EOF) && (feof( infile ) || ferror( infile )) )
break;
fprintf( outfile, " %.2X", in );
if( !(++i % 16) )
{
/* Macro version:
putc( 13, outfile );
putc( 10, outfile );
*/
fputc( 13, outfile ); /* New line */
fputc( 10, outfile );
}
}
}
fcloseall();
exit( 0 );
}