ait-survey

read_binary.cpp

Apr 5th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8.     if(argc!=2)
  9.     {
  10.         cout << "argument error" << endl;
  11.         return 1;
  12.     }
  13.  
  14.     ifstream file (argv[1], ios::in|ios::binary);
  15.     //ifstream fin( outfile, ios::in | ios::binary );
  16.    
  17.     if (!file)
  18.     {
  19.         cout << "Can not open file";
  20.         return 1;
  21.     }
  22.    
  23.     //unsigned int d;
  24.     unsigned char d;   
  25.     int number=0;
  26.    
  27.     while(!(file>>d).eof())
  28.     {
  29.         number=number+1;
  30.         file.read((char *)&d, sizeof(unsigned char) );
  31.        //file.read(&d, sizeof( unsigned char ) );
  32.         //cout << hex << setw(2) << setfill('0') << static_cast<unsigned int>(d);
  33.         cout << number <<":";
  34.         cout << (unsigned int)d << endl;
  35.         //cout << static_cast<unsigned int>(d)<<endl;
  36.         //cout << (int)(int8_t)d<<endl;
  37.         // cout << d << endl;
  38.     }
  39.    
  40.     file.close();
  41.    
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment