小编Len*_*uki的帖子

通过C++读取二进制数据

我尝试通过下面的C++程序读取二进制数据.但它无法显示值.数据保存为8位无符号字符.让我知道如何解决它.

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        cout << "argument error" << endl;
        return 1;
    }

    ifstream file (argv[1], ios::in|ios::binary);
    //ifstream fin( outfile, ios::in | ios::binary );

    if (!file)
    {
        cout << "Can not open file";
        return 1;
    }

    unsigned char d;

    while(!file.eof())
    {
        file.read( ( char * ) &d, sizeof( unsigned char ) );

        cout << d << endl; 
    }

    file.close(); 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ binary

1
推荐指数
1
解决办法
1176
查看次数

将static_cast <char*> malloc/free转换为new/delete

因为与malloc/free相关的分段错误发生,我想将malloc/free转换为new/delete.当malloc/free转换为下面时发生错误.让我知道如何解决它.

(原版的)

char *only_valid_data = static_cast<char*> (malloc (data_size));
Run Code Online (Sandbox Code Playgroud)

(经换算)

char *only_valid_data = new static_cast<char*> [data_size];
Run Code Online (Sandbox Code Playgroud)

c++ malloc static-cast

-6
推荐指数
1
解决办法
888
查看次数

标签 统计

c++ ×2

binary ×1

malloc ×1

static-cast ×1