突发奇想,写了三个小时,可以读取文件并以十六进制形式显示文件内容。
如果打开程序带有参数 -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 length of the file
buffer=new char[length];
file.seekg(0,fstream::beg);//rewind the pointer
file.

本文介绍了如何用C++编写一个程序,该程序能读取文件内容并以十六进制的形式展示。程序允许通过命令行参数指定输出路径,将十六进制内容保存到文件。虽然不支持文件修改功能,但作为读取和显示十六进制内容的基础模块十分有用。
最低0.47元/天 解锁文章
1492

被折叠的 条评论
为什么被折叠?



