- 转自:http://blog.csdn.net/leotnt/article/details/6710843
- /*
- Open an file into binary stream
- Note that argc counts .\binaryfile.exe -o outputFilePath
- So the argc is 3
- argv[0]==.\binaryfile.exe
- argv[1]== -o
- argv[2]== outputFilePath
- */
- #include<iostream>
- #include<fstream>
- #include<iomanip>
- #include<cstring>
- using namespace std;
- int main(int argc,char *argv[])
- {
- char strPath[256]="";
- fstream file;
- char *buffer=NULL;
- int intHex=0;
- int length=0;
- int i,j;
- unsigned long long addr=0;
- cout<<"Enter file path:"<<endl;
- cin>>strPath;
- file.open(strPath,fstream::in|fstream::out|fstream::binary);
- if (!file.is_open()){
- cout<<"Open file error!"<<endl;
- file.close();
- return -1;
- }
- file.seekg(0,fstream::end);
- length=file.tellg();//get the length of the file
- buffer=new char[length];
- file.seekg(0,fstream::beg);//rewind the pointer
- file.read(buffer,length);
- file.close();
- if (argc>1 && !strcmp(argv[1],"-o")){//has an out put argument
- fstream fout;
- fout.open(argv[2],fstream::out);
- if (!fout.is_open()){
- cout<<"Output file error!"<<endl;
- fout.close();
- return -1;
- }
- cout<<"Saving... Please wait..."<<endl;
- fout<<setw(sizeof(long long))<<"Address"<<' ';
- for (i=0;i<16;i++){//print 1-9 A-F markers
- fout<<' ';
- if (i<10) fout<<char(i+'0');
- else fout<<char(i+'A'-10);
- fout<<' ';
- }
- fout<<"ASCII"<<endl;
- for (i=0;i<sizeof(long long);i++) fout<<'0';//first address
- addr+=0x10;
- fout<<' ';
- cout<<"Processing:00.00%";
- fout<<setiosflags(ios::uppercase);
- for (i=1;i<=length;i++){//read the bytes
- cout<<"\b\b\b\b\b\b"
- <<setw(5)
- <<setiosflags(ios::fixed)
- <<setprecision(2)
- <<double(i)/length*100
- <<'\%';
- intHex=buffer[i-1];
- intHex&=0xFF;//change signed char into hex
- if ((intHex&0x0F)&&(intHex&0xF0)) fout<<hex<<intHex;//high bits and low bits !=0
- else if (!(intHex&0xF0)) fout<<'0'<<intHex;//high bits are 0
- else if (!(intHex&0x0F)) fout<<intHex;
- else fout<<"00";//this byte equals 0
- if (i%16) fout<<' ';
- else{
- fout<<' ';
- for (j=i-16;j<i;j++){
- if (buffer[j]>=0x20&&buffer[j]<=0x7E) fout<<buffer[j];
- else fout<<'.';
- }
- fout<<endl;
- fout<<setw(8)<<setfill('0')<<addr<<' ';
- addr+=0x10;
- }
- }
- for (j=0;j<=(16-i%16);j++) fout<<" ";//last line's ASCII
- for (j=i-i%16;j<length;j++){
- if (buffer[j]>=0x21&&buffer[j]<=0x7E) fout<<buffer[j];
- else fout<<'.';
- }
- fout<<endl;
- delete[] buffer;
- return 0;
- }
- else{
- cout<<setw(sizeof(long long))<<"Address"<<' ';
- for (i=0;i<16;i++){//print 1-9 A-F markers
- cout<<' ';
- if (i<10) cout<<char(i+'0');
- else cout<<char(i+'A'-10);
- cout<<' ';
- }
- cout<<"ASCII"<<endl;
- for (i=0;i<sizeof(long long);i++) cout<<'0';//first address
- addr+=0x10;
- cout<<' ';
- cout<<setiosflags(ios::uppercase);
- for (i=1;i<=length;i++){//read the bytes
- intHex=buffer[i-1];
- intHex&=0xFF;//change signed char into hex
- if ((intHex&0x0F)&&(intHex&0xF0)) cout<<hex<<intHex;//high bits and low bits !=0
- else if (!(intHex&0xF0)) cout<<'0'<<intHex;//high bits are 0
- else if (!(intHex&0x0F)) cout<<intHex;
- else cout<<"00";//this byte equals 0
- if (i%16) cout<<' ';
- else{
- cout<<' ';
- for (j=i-16;j<i;j++){
- if (buffer[j]>=0x20&&buffer[j]<=0x7E) cout<<buffer[j];
- else cout<<'.';
- }
- cout<<endl;
- cout<<setw(8)<<setfill('0')<<addr<<' ';
- addr+=0x10;
- }
- }
- for (j=0;j<=(16-i%16);j++) cout<<" ";//last line's ASCII
- for (j=i-i%16;j<length;j++){
- if (buffer[j]>=0x21&&buffer[j]<=0x7E) cout<<buffer[j];
- else cout<<'.';
- }
- cout<<endl;
- delete[] buffer;
- return 0;
- }
- }
-