liboo114 2022-07-04 10:47 采纳率: 50%
浏览 35
已结题

c++读取二进制文件

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

  • 写回答

5条回答 默认 最新

  • Hann Yang 全栈领域优质创作者 2022-07-04 11:19
    关注

    以下这个不错,读入二进制文件,转成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;
        
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 7月14日
  • 已采纳回答 7月6日
  • 创建了问题 7月4日

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM