第二个参数的作用是什么? fwrite() 中的第三个参数?为什么我们需要 3rcount ?

发布于 2025-01-04 01:01:28 字数 394 浏览 0 评论 0原文

您好,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 技术交流群。

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

发布评论

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

评论(3

舞袖。长 2025-01-11 01:01:28

你正在写 COUNT 个东西,每一个的大小都是 SIZE。

因此,如果你有一个包含 50 个 FooBar 结构的数组,你会这样做:

fwrite( some_pointer, sizeof(FooBar), 50, some_stream );

所以你可以总是将 size 设置为 1,然后写:

fwrite( some_pointer, 1, 50 * sizeof(FooBar), some_stream);

但我相信第一种方法,如果没有别的办法,更容易阅读并不易出错。

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:

fwrite( some_pointer, sizeof(FooBar), 50, some_stream );

So you could always set size to 1, and instead write:

fwrite( some_pointer, 1, 50 * sizeof(FooBar), some_stream);

but I believe the first way is, if nothing else, much easier to read and less error prone.

人事已非 2025-01-11 01:01:28

假设您有一个数组:

T a[N]; //T is the type of each element
        //and N is the number of elements in the array
//fill the array here

现在,您想使用 fwrite 写入该数组,因此您将 a 作为第一个参数传递为:

fwrite(a, ..... ); 

现在如何 fwrite< /code> 知道需要写多少字节吗?它无法从第一个参数知道这一点,因为 a 被转换为 const void* ,这会丢失许多有用的信息,例如每个元素的 type在数组 1 中,因此它可以计算每个元素的大小,但是当您将 a 作为第一个参数传递时,此信息就会丢失。因此,您需要传递每个元素的大小,如下所示:

fwrite(a, sizeof(T), ....);

现在 fwrite 知道从中读取元素的地址(或指针),并且它还知道 大小每个元素。但它仍然不知道有多少个元素。因此,您传递数组中的元素数量,如下所示:

fwrite(a, sizeof(T), N, ...);

现在它知道有关数据的所有信息。现在它需要知道的只是文件指针。所以在这里你把它写成:

FILE *fp = /*.....*/ ;
fwrite(a, sizeof(T), N, fp);

希望有帮助。


1.当 a 转换为 const void* 时,它还会丢失另一个有用的信息,即元素的数量。但在 C 中,不能将数组作为参数传递。当你这样做时,它将衰减为指针,丢失有关数组大小的信息。 fwrite 是 C 库中的函数。但是,如果您在 C++ 中使用 fwrite,那么您可以将其包装在函数模板中,如下所示:

template<typename T, size_t N>
size_t fwrite(T (&a)[N], FILE *fp)
{
   return fwrite(a, sizeof(T), N, fp); //call the C function here
}

然后您可以简单地调用它:

int a={1,2,3,4,5,6,7};
double b={10,20,30,40,50,60,70};

fwrite(a,fp); 
fwrite(b,fp); 

看起来不错?但是您不能传递指向此的指针:

float *c = new float[100];

fwrite(c,fp); //compilation error

这是因为我们的 fwrite 函数模板仅接受数组,而不接受指针。

Suppose you've an array as:

T a[N]; //T is the type of each element
        //and N is the number of elements in the array
//fill the array here

Now, you want to write this array using fwrite, so you pass a as first argument as:

fwrite(a, ..... ); 

Now how will fwrite know how many bytes it needs to write? It cannot know this from its first argument because a gets converted into const 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 pass a as first argument. So you need to pass the size of each element as:

fwrite(a, sizeof(T), ....);

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:

fwrite(a, sizeof(T), N, ...);

And now it knows everything about the data. Now all it needs to know is the file-pointer. So here you write it as:

FILE *fp = /*.....*/ ;
fwrite(a, sizeof(T), N, fp);

Hope that helps.


1. When a converts into const 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 using fwrite in C++, then you can wrap it in a function template as:

template<typename T, size_t N>
size_t fwrite(T (&a)[N], FILE *fp)
{
   return fwrite(a, sizeof(T), N, fp); //call the C function here
}

Then you can simply call it as:

int a={1,2,3,4,5,6,7};
double b={10,20,30,40,50,60,70};

fwrite(a,fp); 
fwrite(b,fp); 

Looks good? But you cannot pass a pointer to this:

float *c = new float[100];

fwrite(c,fp); //compilation error

It is because our fwrite function template will accept only array, not pointer.

夜雨飘雪 2025-01-11 01:01:28

Size 是您要写入的每个元素的大小。如果是字节,则应使用 1,否则使用 sizeof(whatever)

计数是您要写入的元素的数量。

Size is the size of each element you want to write. If it's bytes you should use 1, otherwise use sizeof(whatever).

Count is the number of those elements you want to write.

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