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函數的用法示例