fwrite函数的用法示例
C中的fwrite()函数 (fwrite() function in C)
Prototype:
原型:
size_t fwrite(void *buffer, size_t length, size_t count, FILE *filename);
Parameters:
参数:
void *buffer, size_t length, size_t count, FILE *filename
Return type: size_t
返回类型: size_t
Use of function:
使用功能:
The prototype of the function fwrite() is:
函数fwrite()的原型为:
size_t fwrite(void *buffer, size_t length, size_t count, FILE *filename);
In the file handling, through the fwrite() function we write count number of objects of size length to the input stream filename from the array named as buffer. Its returns the number of objects that it will write to the file. If fewer number of objects will be written or EOF is encountered then it will throw an error.
在文件处理中,通过fwrite()函数,我们将大小为长度的对象的计数写入名为缓冲区的数组的输入流文件名中。 它返回将写入文件的对象数。 如果要写入的对象数量较少或遇到EOF ,则它将引发错误。
C语言中的fwrite()示例 (fwrite() example in C)
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *f;
//initialize the arr1 with values
int arr1[5]={1,2,3,4,5};
int arr2[5];
int i=0;
//open the file for write operation
if((f=fopen("includehelp.txt","w"))==NULL){
//if the file does not exist print the string
printf("Cannot open the file...");
exit(1);
}
//write the values on the file
if((fwrite(arr1,sizeof(int),5,f))!=5){
printf("File write error....\n");
}
//close the file
fclose(f);
//open the file for read operation
if((f=fopen("includehelp.txt","r"))==NULL){
//if the file does not exist print the string
printf("Cannot open the file...");
exit(1);
}
//read the values from the file and store it into the array
if((fread(arr2,sizeof(int),5,f))!=5){
printf("File write error....\n");
}
fclose(f);
for(i=0;i<5;i++){
printf("%d\n",arr2[i]);
}
return 0;
}
Output
输出量
翻译自: https://www.includehelp.com/c-programs/fwrite-function-in-c-language-with-example.aspx
fwrite函数的用法示例