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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分