突发奇想,写了三个小时,可以读取文件并以十六进制形式显示文件内容。
如果打开程序带有参数 -o OutputPath 则可以将十六进制形式保存为磁盘文件,否则显示在屏幕上。
代码如下:
/*
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 le