如何FREAD函数的工作? [英] How a fread function work?

查看:133
本文介绍了如何FREAD函数的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()

{


  char  buffer[100];
  fread(buffer,1,4,stdin);
  int i=0;
  while(i<4)

  {
    printf ("%c,\n",buffer[i]);

    i=i+1;
  }

 getch();

}

如何FREAD功能会知道,何时停止读取输入流,什么是大小和数量元素意味着,以及如何操作大小和数量,以获得最大的数据流的读取速度?

How the fread function will know,when to stop reading the input stream,what does the size and count elements signify,and how to manipulate size and count to get the maximum stream reading speed?

推荐答案

要打印的所有字节在 INT ?请记住, INT 是32位的,这是四个字节。读它变成一个字符缓冲区可以更容易地访问这四个字节 INT

To print all bytes in an int? Remember that an int is 32-bit, which is four bytes. Reading it into a char buffer makes it easier to access those four bytes in the int.

编辑:的一点解释的 INT 键入...

Little explanation of the int type...

假设你有一个 INT

int someIntValue = 0x12345678;

本被存储在存储器32位。作为一个字节(字符)是8位,有四个字节到 INT 。每个字节中的 INT 可以通过使用字符数组或指针来访问:

This is stored in 32 bits in the memory. As a single byte (char) is 8 bits, there are four bytes to an int. Each byte in the int can be accessed by using a char array or pointer:

char *someCharPointer = (char *) &someIntValue;

现在,你可以访问这些四个单独的字节,并查看​​它们的值:

Now you can access those four separate bytes, and see their values:

for (int i = 0; i < sizeof(int); i++)
    printf("someCharPointer[%d] = 0x%02x\n", i, someCharPointer[i]);

上面会打印(A little-endian的机器,如86上):

The above will print (on a little-endian machine such as x86):


someCharPointer[0] = 0x78
someCharPointer[1] = 0x56
someCharPointer[2] = 0x34
someCharPointer[3] = 0x12

如果您现在更改 someIntValue 来数 1

someIntValue = 1;

和再打印出来,你会看到的这个的结果是:

and print it out again, you will see this result:


someCharPointer[0] = 0x00
someCharPointer[1] = 0x00
someCharPointer[2] = 0x00
someCharPointer[3] = 0x01

的内存布局的 INT

如果您有类型的变量 INT 存储在内存中以为0x12345678 ,它的存​​储这样的值:

Memory layout of an int

If you have a variable of type int stored in memory with the value 0x12345678, it's stored like this:


   8 bits
,----^---.
|        |

+--------+--------+--------+--------+
|00111000|01010110|00110100|00010010|
+--------+--------+--------+--------+

|                                   |
`-----------------v-----------------'
                  |
               32 bits

INT 是一样的四个字节(或字符 0x78 0x56 0x34 0×12

This int is the same as the four bytes (or char) 0x78, 0x56, 0x34 and 0x12.

但是,如果我们改变 INT 来数 1 那么它存储的是这样的:

However if we change the int to the number 1 then it's stored like this:


   8 bits
,----^---.
|        |

+--------+--------+--------+--------+
|00000000|00000000|00000000|00000001|
+--------+--------+--------+--------+

|                                   |
`-----------------v-----------------'
                  |
               32 bits

INT 是一样的四个字节(或字符 0×00 0×00 0×00 0×01

This int is the same as the four bytes (or char) 0x00, 0x00, 0x00 and 0x01.

所以,现在你希望可以看到作为一个读 INT 和印刷为字符将显示来自不同的结果阅读和 INT 并打印它作为一个 INT

So now you hopefully can see how reading as an int and printing as char will display a different result from reading and int and printing it as an int.

这篇关于如何FREAD函数的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆