Click here to Skip to main content
15,891,828 members
Articles / Programming Languages / C

Dump a file in Windows

Rate me:
Please Sign up or sign in to vote.
1.50/5 (5 votes)
14 Nov 2005CPOL2 min read 34.9K   182   15   1
This program can dump a file to the standard output in Windows.

Introduction

This program can dump a file (either a binary file or a text file) to standard output in a DOS window in Windows. It is a Windows edition for the famous od program in Unix/Linux. It is also a free program that implements a main function like a similar program in Windows, UltraEdit. It can dump a file in 10 different data types.

Implement

This is a simple C program which includes two sub-programs: wod.c and dump2stdout.c. The main program, wod.c, mainly gets user input parameters (dump data type) and a character stream from a file. Then, it transfers these parameters to a sub-program dump2stdout.c. It gets the dump data type predefined in the header file wod.h, which is an enum data structure as follows:

C++
Enum { OD_CHAR, OD_SHORT_INT, OD_LONG, OD_D_CHAR, OD_D_SHORT, 
       OD_D_LONG, OD_ASCII, OD_BIN, OD_FLOAT, OD_DOUBLE } ;

For some potential improvement reasons, the main program divides the whole file into fixed length segments and a tail segment:

Fixed segment length : blk_size = 1024 ( bytes )
Tail segment length  : tail_len = file_len % blk_size ;
Segments count       : blk_no   = file_len / blk_size ;

There is a wired "tell(fd)" in wod.c. I know "read" should move the file pointer forward by one byte, but it seems it does not always work, so I put this redundant function here. For transforming between different data types, I have defined a UNION data structure in wod.h as follows:

C++
Union buf_union {

    unsigned char ubuf [ MAX_BLOCK_SIZE ] ;
    unsigned short int sint [ MAX_BLOCK_SIZE / 2 ] ;
    unsigned long lint [ MAX_BLOCK_SIZE / 4 ] ;
    float ieeef [ MAX_BLOCK_SIZE / 4 ] ;
    double ieeed [ MAX_BLOCK_SIZE / 8 ] ;

} ;

In the sub-program dump2stdout.c, tmpc[16] is used as bit mask in dumping binary, and asc_buf[16] is used for storing temporary ASCII strings in one row. It is the most popular and useful function to dump ASCII. There are three columns in the ASCII dump. The first is the HEX address column, the second is the HEX characters column, and the third is ASCII characters. The variable start_poi is the start offset position of each segment.

C++
case OD_ASCII:

    for ( i = 0 ; i < block_size ; i++ ) {
        if ( i % 16 == 0 ) {
            j = 0 ;  // initialize asc_buf counter j
            if ( i > 0 ) printf ( "  %.16s\n", asc_buf ) ;
            // print 16 ASCII characters
                printf ( "%.8X: ", i + start_poi ) ;
                // print HEX offset address
            
            memset ( asc_buf, '\0', sizeof ( asc_buf ) ) ;
            // clean buffer for asc_buf
        }

        printf ( "%.2x ", buf [ i ] ) ;
        // print 16 HEX characters

        if ( buf [ i ] >= 0x7F || buf [ i ] < 0x20 )
            buf [ i ] = '.' ;
            // print non-printable characters as "."

        asc_buf [ j++ ] = buf [ i ] ;
        // get ASCII value
    }

    l = strlen ( ( const char * ) asc_buf ) ;
    // real length of the last asc_buf

    if ( l < 16 ) {
        for ( m = 0 ; m < 3 * ( 16 - l ) ; m++ ) printf (" ") ;
        // supplement spaces for the last line
    }

    printf ( "  %.16s", asc_buf ) ;
    // print the last ASCII string

    //printf ( "\n%.8X: ", i + start_poi ) ;
    // print separated segment offset if you like
   
    break ;

Compile

This program was compiled in MinGW environment in Windows XP. You have to set the PATH environment variable, or run setp.bat before compiling.

set PATH = %PATH%; C:\MinGW\bin ;

Then, run ccm.bat, or manually compile the program as follows:

g++ -c dump2stdout.c

g++ -c wod.c

g++ wod.o dump2stdout.o -o wod.exe

After all, you can run the program in a DOS window. You can alos redirect the output to a text file for convenience, as follows:

wod test.dat > test.txt

Improvement issues

  1. This pure C program is easy to be transplanted to different platforms.
  2. You can modify the main file wod.c to make this program more flexible. For example, you can add some run-time parameters as position control parameters, such as start position and end position.
  3. You can make the program to output to a text file by replacing "printf" with "fprintf".
  4. You can try to use "lseek64" and "__off64_t" or similar functions to make the program support big files.
  5. You can develop a GUI to wrap the tiny program if you are a stubborn win-style programmer.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
My name is Jiyang Hou (or John Hou). I was born in HeiLongJiang province in north east of China. I got all my educations in China. My university major is Geophysics, but my main professional role is software developer. My biggest accomplishment so far is quit smoking about 5 years ago after almost 20 years smoking history. I am still interested on programming beside making living with it like many other developers. I immigrated to Canada in 2003 and became a permanent resident till now. I live in Calgary, Alberta, Canada. You can reach me by jyhou69@gmail.com regarding to any questions, comments, advice, etc.

Comments and Discussions

 
Generalcheck out GNU TextUtils Pin
Harold Bamford2-Dec-05 5:58
Harold Bamford2-Dec-05 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.