第二个参数的作用是什么? fwrite() 中的第三个参数?为什么我们需要 3rcount ?
您好,fwrite() 的签名如下:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
http://msdn.microsoft. com/en-us/library/h9t88zwz.aspx
我不明白第二个参数“size”和“size”之间的区别。第三个参数“count”?
是否表示从缓冲区复制到文件的数据大小为“size”。计数在这里扮演什么角色? ??
Hi fwrite()'s signature is as follows:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
http://msdn.microsoft.com/en-us/library/h9t88zwz.aspx
I didnt understand the difference between 2nd parameter "size" & 3rd parameter "count"?
Does it mean size of the data copied from buffer to file is of size "size". And what role does count has to play here. ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你正在写 COUNT 个东西,每一个的大小都是 SIZE。
因此,如果你有一个包含 50 个 FooBar 结构的数组,你会这样做:
所以你可以总是将 size 设置为 1,然后写:
但我相信第一种方法,如果没有别的办法,更容易阅读并不易出错。
you're writing COUNT things, each one of which is of size SIZE.
So if you have an array of 50 FooBar structures, you would do:
So you could always set size to 1, and instead write:
but I believe the first way is, if nothing else, much easier to read and less error prone.
假设您有一个数组:
现在,您想使用
fwrite
写入该数组,因此您将a
作为第一个参数传递为:现在如何
fwrite< /code> 知道需要写多少字节吗?它无法从第一个参数知道这一点,因为
a
被转换为const void*
,这会丢失许多有用的信息,例如每个元素的 type在数组 1 中,因此它可以计算每个元素的大小,但是当您将a
作为第一个参数传递时,此信息就会丢失。因此,您需要传递每个元素的大小,如下所示:现在
fwrite
知道从中读取元素的地址(或指针),并且它还知道 大小每个元素。但它仍然不知道有多少个元素。因此,您传递数组中的元素数量,如下所示:现在它知道有关数据的所有信息。现在它需要知道的只是文件指针。所以在这里你把它写成:
希望有帮助。
1.当
a
转换为const void*
时,它还会丢失另一个有用的信息,即元素的数量。但在 C 中,不能将数组作为参数传递。当你这样做时,它将衰减为指针,丢失有关数组大小的信息。fwrite
是 C 库中的函数。但是,如果您在 C++ 中使用fwrite
,那么您可以将其包装在函数模板中,如下所示:然后您可以简单地调用它:
看起来不错?但是您不能传递指向此的指针:
这是因为我们的 fwrite 函数模板仅接受数组,而不接受指针。
Suppose you've an array as:
Now, you want to write this array using
fwrite
, so you passa
as first argument as:Now how will
fwrite
know how many bytes it needs to write? It cannot know this from its first argument becausea
gets converted intoconst void*
which loses lots of useful information, such as the type each elements in the array1, thereby it could have calculated the size of each element, but this information is lost the moment you passa
as first argument. So you need to pass the size of each element as:Now
fwrite
knows the address (or pointer) from where it will read the elements, and it also knows the size of each element. But it still doesn't know how many elements. So you pass the number of elements in the array, as:And now it knows everything about the data. Now all it needs to know is the file-pointer. So here you write it as:
Hope that helps.
1. When
a
converts intoconst void*
, it also loses another useful information which is the number of elements. But in C, you cannot pass an array as argument. When you do so, it will decay into pointer, losing the information about the size of the array.fwrite
is a function from C library. However, if you're usingfwrite
in C++, then you can wrap it in a function template as:Then you can simply call it as:
Looks good? But you cannot pass a pointer to this:
It is because our
fwrite
function template will accept only array, not pointer.Size 是您要写入的每个元素的大小。如果是字节,则应使用
1
,否则使用sizeof(whatever)
。计数是您要写入的元素的数量。
Size is the size of each element you want to write. If it's bytes you should use
1
, otherwise usesizeof(whatever)
.Count is the number of those elements you want to write.