【问题标题】:Reading a binary file into a global unsigned int将二进制文件读入全局无符号整数
【发布时间】:2019-11-23 01:14:53
【问题描述】:

我正在尝试在 C 中启动 VM,并且我正在尝试了解如何读取我从汇编程序生成的二进制文件。根据分配指令,我们将分配一个全局内存作为 1k 内存空间的无符号字节,然后使用加载函数将这个二进制文件读入内存,然后使用 fetch 函数将这些字节读入它们的指令中。我遇到的问题是第 1 部分,我如何将这个二进制文件读入这个无符号整数数组,然后以一种可以使用的方式对其进行解码?目前,我所打印的值根本不是预期的值。

MAIN.c

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

unsigned int memory[1028];

int loads(char *filename){
     FILE *file = fopen(filename, "r");
     ssize_t read;
     if(file == NULL){
         exit(-1);
     }

     while(fread(&memory, sizeof(unsigned int), 1, file) == 1){
         printf("%d\n", *memory);
     }
    fclose(file);
    exit(0);
}

int main(int argc, char** argv){
    if (argc <= 1){
        printf("No file found\n");
        return -1;
    }

    char *filename = argv[1];

    loads(filename);
}

inputfile.txt

t@w@#(这是它显示的不可读的内容,但在使用 od -t x1 output.txt | head -5 时会打印出 0000000 74 40 77 40 11 23 0000006

电流输出 1081557108

所需的输出 74 40 77 40 11 23

【问题讨论】:

  • 您一次读取 4 个字节 (sizeof(unsigned int))。也许你想要unsigned char
  • 好主意,试一试,它仍然产生了意想不到的输出,但可惜不同。虽然这是一个更好的地方,但我以前和现在一样,一旦我将值降低到正确的值,我就可以更轻松地访问它们。这个输出是116 64 119 64 17 35 中间的空格实际上是\n
  • 您的输入是十六进制的。 0x74 == 1160x40 == 64 等等。使用printf("%02x ", ...)
  • 啊,好吧,所以我想这就是解码移植的来源。这是有道理的。
  • @Jester 把它写下来作为答案,我会给 +1。

标签: c binaryfiles


【解决方案1】:

使用fread 读取时,fread 读取字节。它不知道线的任何概念。字节 '\n' (0xa) 与文件中的任何其他字节一样只是一个字节。 fread 的声明是:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

其中ptr 是足以容纳来自streamsize * nmemb(大小 * 成员数)值的内存块的起始地址。其中size 是元素或对象大小(在您的情况下sizeof (unsigned)nmemb 是您将读取的数字(例如1028 在您的unsigned memory[1028]; 情况下)参见man 3 fread

您的代码有问题的地方是您使用&amp;memory 作为指针。这是不正确的。 (以及为什么你得到 2 值)&amp;memory 是类型 unsigned (*)[1028](例如 pointer-to-array-of unsigned[1028]sizeof(unsigned) 是什么? (提示:4-bytessizeof (a_pointer) 是什么? (提示:8-bytes 在 x86_64 上)。因此,您可以将 2-unsigned 值存储在指针的存储中,同时调用 Undefined Behavior 与读取的其余部分。

fread 的正确参数是简单的 memory,它作为一个数组被转换为访问指针,受C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3) 中列出的四个异常的影响

在您的情况下,您将一个无符号数组 memory 声明为:

#define MAXU 1028   /* if you need a constant, #define one (or more) */

unsigned memory[MAXU];

(注意:除非绝对必要,否则您要避免使用 全局 变量。相反,在所需范围内声明数组,例如在 main() 中,然后将指针传递给需要的任何函数)

处理文件时,不是将文件名作为参数传递给函数,而是打开文件并验证它是否在调用者中打开(此处为main()),并将打开的FILE*指针作为参数传递.除非文件可以打开,否则不需要进行函数调用和设置函数堆栈。因此,在main() 中,您可以执行类似于以下的操作,将文件名作为第一个参数传递给您的程序,例如

int main(int argc, char **argv) {

    int n = 0;

    if (argc < 2) { /* validate at least 1 argument given for filename */
        fprintf (stderr, "usage: %s filename\n", argv[0]);
        return 1;   /* do NOT return negative values to the shell */
    }

    /* use filename provided as 1st argument */
    FILE *fp = fopen (argv[1], "r");

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

注意:不要向shell返回负值。而是返回1EXIT_FAILURE(其值为1)表示错误)

现在你想调用你的 loads 函数来传递打开的文件流 fp,例如

    if ((n = loads (fp)) == 0) {    /* validate return of loads */
        fputs ("error: loads() read zero bytes or error occurred.\n", stderr);
        return 1;
    }

您的loads() 函数简化为:

int loads (FILE *fp)
{
    return fread (memory, sizeof *memory, MAXU, fp);
}

(注意: fread返回读取的成员数,只等于size == 1时读取的字节数。所以通过选择unsigned的大小会返回读取的unsigned 值的数量)。

一个完整的例子可以是:

#include <stdio.h>
#include <stdlib.h>

#define MAXU 1028   /* if you need a constant, #define one (or more) */

unsigned memory[MAXU];

int loads (FILE *fp)
{
    return fread (memory, sizeof *memory, MAXU, fp);
}

int main(int argc, char **argv) {

    int n = 0;

    if (argc < 2) { /* validate at least 1 argument given for filename */
        fprintf (stderr, "usage: %s filename\n", argv[0]);
        return 1;   /* do NOT return negative values to the shell */
    }

    /* use filename provided as 1st argument */
    FILE *fp = fopen (argv[1], "r");

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    if ((n = loads (fp)) == 0) {    /* validate return of loads */
        fputs ("error: loads() read zero bytes or error occurred.\n", stderr);
        return 1;
    }

    for (int i = 0; i < n; i++) {   /* loop n times outputting values */
        if (i && i % 10 == 0)       /* output 10 columns for convenience */
            putchar ('\n');
        printf (" %4x", memory[i] & 0xffff);  /* & 0xffff is for my file */
    }
    putchar ('\n');     /* tidy up with newline */
}

使用/输出示例

在我的文件 ../dat/100000int.bin 中,我有 100,000 个整数值(在 short 范围内的正负都有,所以我用 memory[i] &amp; 0xffff 屏蔽了每个值的高 2 字节以防止 符号- 扩展当存储的值小于零时输出为无符号值,例如0xffff7d77

$ ./bin/freadunsignedval ../dat/100000int.bin
 7d77 6cad c544 21f8 723f 54d1 8a81 2c6a 1ba9 f95b
 1858 7565  f4b 28e4 7fdd 5a92 b5df 7a3f 4e1a 7e19
  669 f365 34c0  95e  903 689d 66f2 abf2 1223 1290
 372f  f9b 7f3d 71eb ce6d 717c 46bc 2712 1de6 6265
 d248 363e 57cb 3d03 5f23 57a8 1795 2944 51e7 65af
 275d 5851 724a 5c1e 61af 7b4d 44bb 48a2 4f5b 56de
 5b32  68b 6679 5a6f 7876 180c 4beb 3f33 3f1f 69d1
 2198 6cd7 200f 7963 29da 7f32 510b 4170 2877 22f3
 271f 4fd4 84bc 196a 2bf2 5cf3 14b7 70ad 2595 6413
...
 6503   b2 f135 15f6 776c b7f3 1ffd 1365 1e4d 129b
  23f 6c3e  20c  a8c 2ef6 f72b  4d4 793a 1b6b  425
 79d5 6bac  ba8 6527 6239 17ea 644e 1175 4464 1c88
 346d 2967 1d3a 4339 3f5d 14a6  b46 5f5a

(即 103 行输出,每行 10 个值,最后一行 8 个值)

查看一下,如果您还有其他问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    相关资源
    最近更新 更多