c语言中fread函数_使用示例的C语言中的fread()函数

c语言中fread函数

C语言中的fread()函数 (fread() function in C)

Prototype:

原型:

    size_t fread(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 fread() is:

函数fread()的原型为:

    size_t fread(void *buffer, size_t length, size_t count, FILE *filename);

In the file handling, through the fread() function, we read the count number of objects of size length from the input stream filename to the array named buffer. Its returns the number of objects being read from the file. If lesser no of objects are read or EOF is encountered before then it will give an error.

在文件处理中,通过fread()函数 ,我们从输入流文件名到名为buffer的数组读取大小为长度的对象的计数 。 它返回从文件中读取的对象数。 如果较少的对象没有被读取或在此之前遇到EOF ,则它将给出错误。

C语言中的fread()示例 (fread() 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);
	
	printf("The array content is-\n");
	for(i=0;i<5;i++){
		printf("%d\n",arr2[i]);
	}
	
	return 0;
}

Output

输出量

fread example in c

翻译自: https://www.includehelp.com/c-programs/fread-function-in-c-language-with-example.aspx

c语言中fread函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值