fread函数

size_t fread(void* buffer,size_t size,size_t count,FILE* stream);
功 能:
从一个文件流中读数据,读取count个元素,每个元素size字节.如果调用成功返回count.返回实际读取size*count字节.如不成功,返回实际读取的元素个数
参 数:
buffer
用于接收数据内存地址,大小至少是size*count字节.
size
单个元素的大小,单位是字节
count
元素的个数,每个元素是size字节.
stream
输入流
返回值:
实际读取的元素数.如果返回值与count(不是count*size)不相同,则可能文件结尾或发生错误.
从ferror和feof获取错误信息或检测是否到达文件结尾.

程序例

#include <stdio.h>
#include <string.h>
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, 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);
return 0;
}
posted @ 2013-04-11 11:00  米其林轮船  阅读(169)  评论(0)    收藏  举报