使用fread()读取二进制文件内容

发布于 2025-01-14 08:43:25 字数 1134 浏览 0 评论 0原文

我想使用 fread 读取二进制文件的一段。二进制内容为 0xf3 0xf 0x1e 0xfa...

在此处输入图像描述

但我从 fread() 0xfffffff3 0xf 读取并输出...我想知道为什么0xf3 变为 0xfffffff3 0xf 0x1e 0xfffffffa...

在此输入图片描述

我的实现代码如下:

    char movslq_inst[length];

    /* Open the target_binary */
    
    FILE * target_binary = fopen(target_path,"rb+");

    /* Jump to target address */
    if (fseek(target_binary, offset, SEEK_SET) != 0)
    {  
        printf("Unable to seek file '%s' at offset '%i", target_path, offset);
        exit(0);
    }

    /* Read the target bytes */
    if (fread(movslq_inst, length, 1, target_binary) != 1)
    { 
        printf("Unable to read file '%s'", target_path);
        exit(0);
    }

    /* test examine */
    printf("read content: \n");
    for(int i = 0; i < length; i++)
    {
        printf("0x%x ",movslq_inst[i]);
    }
    printf("\n");

I want to use fread to read a segment of a binary file. The binary content is 0xf3 0xf 0x1e 0xfa...

enter image description here

But I read and output from fread() 0xfffffff3 0xf... I want to know why 0xf3 becomes 0xfffffff3 0xf 0x1e 0xfffffffa...

enter image description here

My implementation code is as follows:

    char movslq_inst[length];

    /* Open the target_binary */
    
    FILE * target_binary = fopen(target_path,"rb+");

    /* Jump to target address */
    if (fseek(target_binary, offset, SEEK_SET) != 0)
    {  
        printf("Unable to seek file '%s' at offset '%i", target_path, offset);
        exit(0);
    }

    /* Read the target bytes */
    if (fread(movslq_inst, length, 1, target_binary) != 1)
    { 
        printf("Unable to read file '%s'", target_path);
        exit(0);
    }

    /* test examine */
    printf("read content: \n");
    for(int i = 0; i < length; i++)
    {
        printf("0x%x ",movslq_inst[i]);
    }
    printf("\n");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-01-21 08:43:25

我解决了。它应该是 unsigned char movslq_inst[length]; 而不是

char movslq_inst[length];

I worked it out. It should be unsigned char movslq_inst[length]; instead of

char movslq_inst[length];.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文