fread函数c语言
发布时间: 2025-02-19 22:14:29
Ⅰ 关于C语言fread的用法
简介
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("mmy.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;
}
热点内容