读取二进制文件

发布于 2024-11-10 06:52:17 字数 618 浏览 4 评论 0原文

所以我试图读取已提供的文件系统磁盘。

所以,我想做的是从文件系统读取 1044 字节。我目前正在做的事情如下:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);        //Goes to 1024th byte
  int check[sizeof(char)*4];        //creates a buffer array 4 bytes long
  fread(check, 1, 4, fp);           //reads 4 bytes from the file
  printf("%d",check);               //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

检查应该打印的值是1。但是我得到的负值在每次运行文件时都会不断变化。我不明白我做错了什么。我是否采取了正确的方法来读取磁盘的字节并打印它们。

我基本上想做的是读取磁盘的字节,并读取某些字节的值。这些字节是可以帮助我理解磁盘结构/格式的字段。

任何帮助将不胜感激。 谢谢。

so I am trying to read a filesystem disk, which has been provided.

So, what I want to do is read the 1044 byte from the filesystem. What I am currently doing is the following:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);        //Goes to 1024th byte
  int check[sizeof(char)*4];        //creates a buffer array 4 bytes long
  fread(check, 1, 4, fp);           //reads 4 bytes from the file
  printf("%d",check);               //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

The value that check should be printing is 1. However I am getting negative values which keep changing everytime I run the file. I don't understand what I am doing wrong. Am I taking the right approach to reading bytes of the disk, and printing them.

What I basically want to do is read bytes of the disk, and read the values at certain bytes. Those bytes are fields which will help me understand the structure/format of the disk.

Any help would be appreciated.
Thank you.

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

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

发布评论

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

评论(2

放飞的风筝 2024-11-17 06:52:18

这一行:

int check[sizeof(char)*4];

分配一个 4 个 int 的数组。

因此检查的类型是 int*,因此这一行:

printf("%d",check);

打印数组的地址。

你应该做什么 将它分配为 int:

int check;

然后读取它:

fread(&check, 1, sizeof(int), fp);

(顺便说一下,这段代码假设 int 是 4 个字节。)

This line:

int check[sizeof(char)*4];

allocates an array of 4 ints.

The type of check is therefore int*, so this line:

printf("%d",check);

prints the address of the array.

What you should do it allocate it as an int:

int check;

and then fread into it:

fread(&check, 1, sizeof(int), fp);

(This code, incidentally, assumes that int is 4 bytes.)

我不咬妳我踢妳 2024-11-17 06:52:18
int check[sizeof(char)*4];    //creates a buffer array 4 bytes long

这是不正确的。您正在创建一个由四个整数组成的数组,每个整数通常为 32 位,然后当您 printf("%d",check) 时,您将打印该数组的地址,该地址可能会在您每次运行程序时发生变化。我认为你想要的是这样的:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);         //Goes to 1024th byte
  int check;                         //creates a buffer array the size of one integer
  fread(&check, 1, sizeof(int), fp); //reads an integer (presumably 1) from the file
  printf("%d",check);                //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

请注意,你不是声明一个整数数组,而是只声明一个。另请注意从 fread(check, ...) 到 fread(&check, ...) 的更改。 fread 的第一个参数是缓冲区的地址(在本例中,您想要读取数据的单个整数)。

请记住,虽然整数可能有 32 位长,但这并不能保证。另外,在大多数操作系统中,整数首先以最低有效字节存储在磁盘上,因此如果磁盘上的数据在字节 1044 处如下所示,则您只会读取 1:

0x01 0x00 0x00 0x00

如果是相反,则为 0x00 00 00 01,将被读取为 16777216 (0x01000000)。

如果你想读取多个整数,你可以使用一个数组,如下所示:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);         //Goes to 1024th byte
  int check[10];                     //creates a buffer of ten integers
  fread(check, 10, sizeof(int), fp); //reads 10 integers into the array
  for (int i = 0; i < 10; i++)
    printf("%d ", check[i]);         //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

在这种情况下,check(不带括号)是一个指向数组的指针,这就是为什么我将 fread 改回 fread(check, . ..)。

希望这有帮助!

int check[sizeof(char)*4];    //creates a buffer array 4 bytes long

This is incorrect. You are creating an array of four integers, which are typically 32 bits each, and then when you printf("%d",check) you are printing the address of that array, which will probably change every time you run the program. I think what you want is this:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);         //Goes to 1024th byte
  int check;                         //creates a buffer array the size of one integer
  fread(&check, 1, sizeof(int), fp); //reads an integer (presumably 1) from the file
  printf("%d",check);                //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

Note that instead of declaring an array of integers, you are declaring just one. Also note the change from fread(check, ...) to fread(&check, ...). The first parameter to fread is the address of the buffer (in this case, a single integer) into which you want to read the data.

Keep in mind that while integers are probably 32 bits long, this isn't guaranteed. Also, in most operating systems, integers are stored with the least significant byte first on the disk, so you will only read 1 if the data on the disk looks like this at byte 1044:

0x01 0x00 0x00 0x00

If it is the other way around, 0x00 00 00 01, that will be read as 16777216 (0x01000000).

If you want to read more than one integer, you can use an array as follows:

if (fp  = fopen("filesysFile-full", "r")) {
  fseek(fp, 1044, SEEK_SET);         //Goes to 1024th byte
  int check[10];                     //creates a buffer of ten integers
  fread(check, 10, sizeof(int), fp); //reads 10 integers into the array
  for (int i = 0; i < 10; i++)
    printf("%d ", check[i]);         //prints 
  int close = fclose(fp);
  if (close == 0) {
    printf("Closed");
  }
}

In this case, check (without brackets) is a pointer to the array, which is why I've changed the fread back to fread(check, ...).

Hope this helps!

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