c语言fread函数定义,c语言的fread函数

关于C语言fread的用法

简介

创新互联公司是一家以网站建设公司、网页设计、品牌设计、软件运维、seo优化排名、小程序App开发等移动开发为一体互联网公司。已累计为成都火锅店设计等众行业中小客户提供优质的互联网建站和软件开发服务。

fread

能:

从一个流中读数据

函数原型:

size_t

fread(

void

*buffer,

size_t

size,

size_t

count,

file

*stream

); 

数:

1.用于接收数据的地址(指针)(buffer)

2.单个元素的大小(size)

:单位是字节而不是位,例如读取一个整型数就是2个字节

3.元素个数(count)

4.提供数据的文件指针(stream)

返回值:成功读取的元素个数

程序例

#include

int

main(void)

{

file

*stream;

char

msg[]

=

"this

is

a

test";

char

buf[20];

if

((stream

=

fopen("dummy.fil",

"w+"))

==

null)

{

fprintf(stderr,

"cannot

open

output

file.\n");

return

1;

}

/*

write

some

data

to

the

file

*/

fwrite(msg,

strlen(msg)+1,

1,

stream);

/*

seek

to

the

beginning

of

the

file

*/

fseek(stream,

0,

seek_set);

/*

read

the

data

and

display

it

*/

fread(buf,

strlen(msg)+1,

1,stream);

printf("%s\n",

buf);

fclose(stream);

return

0;

}

C语言 fread函数每次都读取文件指定个字符长度应该怎样定义?

size=sizeof(char);

nitems=4;

其中sizeof(char)==1;

事实上int fread(void *ptr,int size,int nitems,FILE *stream);

读取字符数为size*nitems个字符

只要有size*nitems==4即可

也就是说有

size=1;

nitems=4;

size=2;

nitems=2;

size=4;

nitems=1;

c语言fread函数的用法

C语言中:fread是一个函数。从一个文件流中读数据,最多读取count个元素,每个元素size字节,如果调用成功返回实际读取到的元素个数,如果不成功或读到文件末尾返回 0。下面我们来看看c语言fread函数的用法。

fread()函数---- Reads data from a stream.

#includestdio.h

size_t fread( void *buffer, size_t size, size_t count,FILE *stream );

从一个文件流中读数据,读取count个元素,每个元素size字节.如果调用成功返回count.如果调用成功则实际读取size*count字节

buffer的大小至少是 size*count 字节.

return:

fread returns the number of full items actually read

实际读取的元素数.如果返回值与count(不是count*size)不相同,则可能文件结尾或发生错误.

从ferror和feof获取错误信息或检测是否到达文件结尾.

DEMO:

[cpp] view plain#include stdio.h

#include process.h

#include string.h

int main()

{

FILE *stream;

char msg[]="this is a test";

char buf[20];

if ((stream=fopen("dummy.fil","w+"))==NULL)

{

fprintf(stderr,"cannot open output file.\n");

return 1;

}

/*write some data to the file*/

fwrite(msg,1,strlen(msg)+1,stream);

/*seek to the beginning of the file*/

fseek(stream,0,SEEK_SET);

/*read the data and display it*/

fread(buf,1,strlen(msg)+1,stream);

printf("%s\n",buf);

fclose(stream);

system("pause");

return 0;

}

DEMO2

[cpp] view plainint main(void)

{

FILE *stream;

char list[30];

int i,numread,numwritten;

/*open file in text mode:*/

if ((stream=fopen("fread.out","w+t"))!=NULL)

{

for (i=0;i25;i++)

{

list[i]=(char)('z'-i);

}

/*write 25 characters to stram*/

numwritten=fwrite(list,sizeof(char),25,stream);

printf("Wrote %d items\n",numwritten);

fclose(stream);

}

else

printf("Problem opening the file\n");

if ((stream=fopen("fread.out","r+t"))!=NULL)

{

numread=fread(list,sizeof(char),25,stream);

printf("Number of items read =%d\n",numread);

printf("Contents of buffer=%.25s\n",list);

fclose(stream);

}

else

{

printf("File could not be opened\n");

}

system("pause");

return 0;

}

c语言中fread函数怎么用

c语言中fread函数语法为size_t fread( void *restrict buffer, size_t size, size_t count, FILE *restrict stream )。buffer是指向要读取的数组中首个对象的指针,size是每个对象的大小(单位是字节),count是要读取的对象个数,stream是输入流。通过fread函数可进行数据读取,返回成功读取的对象个数。

扩展资料:

fread函数从给定输入流stream读取最多count个对象到数组buffer中(相当于以对每个对象调用size次fgetc),把buffer当作unsignedchar数组并顺序保存结果。流的文件位置指示器前进读取的字节数。

若出现错误,则流的文件位置指示器的位置不确定。若没有完整地读入最后一个元素,则其值不确定,可能小于count。若size或count为零,则fread返回零且不进行其他动作。fread不区分文件尾和错误,因此调用者必须用feof和ferror才能判断发生了什么。

fread(&w,4,1,out)是什么意思

编辑本段C语言库函数名:

简介

fread 功 能: 从一个流中读数据 函数原型: size_tfread(void*buffer,size_tsize,size_tcount,FILE*stream);  参 数: 1.用于接收数据的地址(指针)(buffer) 2.单个元素的大小(size) :单位是字节而不是位,例如读取一个int型数据就是4个字节 3.元素个数(count) 4.提供数据的文件指针(stream) 返回值:读取的元素的个数

这个是引用的百度百科里面的内容,详细地址 是:

还有例子说明,详细看一下你就会明白了

c语言文件操作fwrite和fread

fread是C语言标准为中的一个函数。它从一个文件流中读数据,最多读取count个元素,每个元素size字节,如果调用成功返回实际读取到的元素个数,如果不成功或读到文件末尾返回 0。

fwrite是C语言标准库中的一个函数,指向文件写入一个数据块。示例如下:

//读取一个完整的文件

#include stdio.h

#include stdlib.h

int main()

{

FILE* pFile;   //文件指针

long lSize;   // 用于文件长度

char* buffer; // 文件缓冲区指针

size_t result;  // 返回值是读取的内容数量

pFile = fopen("myfile.bin" , "rb");

if (pFile == NULL) {fputs("File error", stderr); exit(1);}    // 如果文件错误,退出1

// 获得文件大小

fseek(pFile , 0 , SEEK_END); // 指针移到文件末位

lSize = ftell(pFile);  // 获得文件长度

rewind(pFile);  // 函数rewind()把文件指针移到由stream(流)指定的开始处, 同时清除和流相关的错误和EOF标记

// 为整个文件分配内存缓冲区

buffer = (char*) malloc(sizeof(char) * lSize); // 分配缓冲区,按前面的 lSize

if (buffer == NULL) {fputs("Memory error", stderr); exit(2);}  // 内存分配错误,退出2

//  该文件复制到缓冲区

result = fread(buffer, 1, lSize, pFile); // 返回值是读取的内容数量

if (result != lSize) {fputs("Reading error", stderr); exit(3);} // 返回值如果不和文件大小,读错误

// terminate // 文件终止

fclose(pFile);

free(buffer);

return 0;

}

综合使用的例子。

#include stdio.h

int main()

{

FILE* pFile;

float buffer[] = { 2.0 , 3.0 , 8.0 };

pFile = fopen("myfile.bin" , "wb"); // 打开文件写操作

fwrite(buffer , 1 , sizeof(buffer) , pFile); // 把浮点数组写到文件 myfile.bin

fclose(pFile); // 关闭文件

float read[3];

pFile = fopen("myfile.bin" , "rb"); // 重新打开文件读操作

fread(read , 1 , sizeof(read) , pFile); // 从文件中读数据

printf("%f\t%f\t%f\n", read[0], read[1], read[2]);

fclose(pFile); // 关闭文件

return 0;

}

当前题目:c语言fread函数定义,c语言的fread函数
网站网址:https://www.cdcxhl.com/article40/hspeeo.html

成都网站建设公司_创新互联,为您提供网站导航Google营销型网站建设小程序开发移动网站建设微信公众号

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站建设