home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource1
/
cenvew
/
hexdump.cmm
< prev
next >
Wrap
Text File
|
1993-08-30
|
3KB
|
105 lines
/**********************************************************************
*** HexDump.bat - Hexidecimal dump of the contents of a file. This ***
*** examples program uses the CEnvi file routines. ***
*** It calls upon NotePad to display the results of ***
*** the dump. ***
**********************************************************************/
#include <MsgBox.lib>
#include <PickFile.lib>
main(argc,argv)
{
// prompt for file name if file wasn't passed in
if ( argc == 1 ) {
FileName = PromptForFile();
} else {
if ( argc != 2 )
Instructions();
FileName = argv[1];
}
if ( NULL == FileName || 0 == FileName[0] )
Instructions();
// get the name of the temporary destination file to write to
DumpFileName = tmpnam();
// perform the dump operation. True for success else false
if ( FileDump(FileName,DumpFileName) ) {
// dump worked OK, so spawn notepad to view the result
spawn(P_WAIT,"NotePad.exe",DumpFileName);
}
// remove the temporary file, whether it exists or not
remove(DumpFileName);
return(EXIT_SUCCESS);
}
FileDump(SrcFile,DstFile)
{
success = FALSE; // assume failure
srcFP = fopen(SrcFile,"rb");
if ( NULL == srcFP )
Error("Could not open file %s for reading.",SrcFile);
else {
dstFP = fopen(DstFile,"wt");
if ( NULL == dstFP )
Error("Could not open file %s for writing.");
else {
fprintf(dstFP,"Hexidecimal dump of %s\n",SrcFile);
Unprintables = "\a\b\t\r\n\032\033"
success = TRUE;
for ( offset = 0; 0 < (count = fread(data,16,srcFP)); offset += 16 ) {
// display hex offset in file
fprintf(dstFP,"%06X ",offset)
// display hex value for each number
for ( i = 0; i < count; i++ )
fprintf(dstFP,"%02X ",data[i])
// fill in any extra gaps if count < 16
while( i++ < 16 )
fprintf(dstFP," ")
// display ascii value for each printable character
// substitute a period for unprintable characters
data[count] = 0; // string must be null-terminated
while( (UnprintableIndex = strcspn(data,Unprintables)) < count )
data[UnprintableIndex] = '.';
fprintf(dstFP," %s\n",data)
if ( count < 16 )
break
}
fclose(dstFP);
}
fclose(srcFP);
}
return(success);
}
PromptForFile()
{
return(GetOpenFileName(NULL,"HexDump File Selection"));
}
Error(FormatString,arg1,arg2,arg3/*etc...*/)
{
va_start(valist,FormatString);
vsprintf(message,FormatString,valist);
va_end(valist);
MessageBox(message);
}
Instructions()
{
MessageBox(
"HexDump.cmm - This is a Cmm program to display a hexidecimal dump of\n"
" any file. Either execute with one argument, whcih is the\n"
" name of the file to view, or with no arguments to be\n"
" prompted for a file name."
);
exit(EXIT_FAILURE);
}