Display contents of specified file in both ASCII and in hex. : File Utility « File « C++






Display contents of specified file in both ASCII and in hex.

 

#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[])
{
  if(argc != 2) {
    cout << "Usage: Display <filename>\n";
    return 1;
  }

  ifstream in(argv[ 1 ], ios::in | ios::binary);

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  register int i, j;
  int count = 0;
  char c[16];

  cout.setf(ios::uppercase);
  while(!in.eof()) {
    for(i = 0; i < 16 && !in.eof(); i++) {
      in.get(c[i]);
    }
    if(i <16) 
       i--;                                 // get rid of eof

    for(j = 0; j < i; j++)
      cout << setw(3) << hex << (int) c[j];
    for(; j < 16; j++) 
      cout << "   ";

    cout << "\t";
    for(j = 0; j < i; j++)
      if(isprint(c[ j ])) 
         cout << c[ j ];
      else 
         cout << ".";

    cout << endl;

    count++;
    if(count == 16) {
      count = 0;
      cout << "Press ENTER to continue: ";
      cin.get();
      cout << endl;
    }
  }

  in.close();

  return 0;
}


           
         
  








Related examples in the same category

1.Read and display a text file line by line.
2.Create a file comparision utility.
3.Copy a text file and display number of chars copied.
4.Word count for input file: file read with ifstream
5.Copy files
6.Copy a file and display number of chars copied.
7.Concatenate files
8.Swap characters in a file.
9.Swap characters in a file with error checking.
10.Copy a file and reverse case of letters.
11.Count letters.
12.Copy a file and reverse case of letters with error checking.
13.Copy and convert tabs to spaces.
14.Search file.