c++读取二进制文件

c++,在已有一个test.bin文件,怎么读出这个文件的内容并输出

以下这个不错,读入二进制文件,转成16进制 写入一个文本文件

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

using namespace std;

char HEX[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };

void transfrom(int num, char* hexNumber)
{
    for (int i = 0; i < 8; i++)
    {
        hexNumber[i] = '0';
    }
    int index = 7;
    while (num != 0 && index >= 0)
    {
        hexNumber[index--] = HEX[num % 16];
        num = num / 16;
    }
}

string getFileName(string& filename)
{
    int index = -1;
    for (int i = filename.length() - 1; i >= 0; i--)
    {
        if (filename[i] == '\\')
        {
            index = i;
            break;
        }
    }
    return filename.substr(index + 1, filename.length());
}

int main()
{
    int num = 0;    //The Byte have been read
    string path_r;
    string path_w;
    ifstream in;
    ofstream out;

    cout << "Please input the File for read and write's name: " << endl;

    while (true)
    {
        cout << "The file path to read: ";
        getline(cin, path_r);
        in = ifstream(path_r, ios::binary);
        if (!in.is_open())
        {
            cout << "Error: File Path is Wrong" << endl;
        }
        else break;
    }
    // Get the file path to Write
    cout << "The File Path to save(.txt): ";
    getline(cin, path_w);
    out = ofstream(path_w);

    //Get the File size
    long long Beg = in.tellg();
    in.seekg(0, ios::end);
    long long End = in.tellg();
    long long fileSize = End - Beg;
    in.seekg(0, ios::beg);
    out << "\t\t" << getFileName(path_r) << "\tFile Size: " << fileSize / 1024.0 <<     "KB" << endl << endl;

    //The index of every row
    char hexNumber[9] = "00000000";

    //Print the first row's index
    unsigned char temp;
    out << "\t\t"; //Format index
    for (int i = 0; i < 16; i++) out << HEX[i] << "  ";
    out << endl;

    //Read and Write File
    while (in.read((char*)&temp, 1))
    {
        if (num % 16 == 0)
        {
            out << endl;
            transfrom(num, hexNumber);
            out << hexNumber << ":\t";
        }
        num++;
        int hex = (unsigned)temp;
        char a = HEX[hex / 16];
        char b = HEX[hex % 16];
        out << a << b << " ";
    }

    out.seekp(0,ios::beg);
    
    // Close file
    in.close();
    out.close();

    cout << "Read Successfully" << endl;
    system("pause");
    return 0;
    
}

用二进制格式打开文件,然后按照二进制文件的数据组织方式进行读取

使用FILE 类来进行读取
fread


#include <stdio.h>
#include <string.h>
FILE *f;
char buffer[4096];
int r,a;
void HexDump(char *buf,int len,int addr) {
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}
int main(int argc,char **argv) {
    if (argc<2) {
        fprintf(stderr,"Usage: %s filename.ext\n",argv[0]);
        return 2;
    }
    f=fopen(argv[1],"rb");
    if (NULL==f) {
        fprintf(stderr,"Can not open file [%s]!\n",argv[1]);
        return 1;
    }
    a=0;
    while (1) {
        r=fread(buffer,1,4096,f);
        HexDump(buffer,r,a);
        a+=r;
        if (r<4096) break;
    }
    fclose(f);
    return 0;
}

看下这篇博客,也许你就懂了,链接:C++ 读取二进制文件