fread 函数怎么用

goodluck_sun 2012-04-16 04:22:39


fread返回的到底是什么啊??求详解!!
。。。。。。。。。。。。
switch (fread(buf, 1, 3, fp))
{
case 3: // bytes of UTF8 BOM
if (*(unsigned long*)buf == BOM_UTF8)
{
encoding = ENCODING_UTF8;
}
//break;
case 2: // bytes of UTF16LE BOM
if (*(unsigned short*)buf == BOM_UTF16LE)
{
encoding = ENCODING_UTF16LE;
}
break;
default:
break;
}
...全文
367 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxnuzhouguohong 2012-04-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

fread
Reads data from a stream.

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

Function Required Header Compatibility
fread <stdio.h> ANSI, Win 95, Win NT


For a……
[/Quote]


直接MSDN,经典
Ever_lover 2012-04-16
  • 打赏
  • 举报
回复
百度百科,google也行不比csdn更好
zhoujiawen 2012-04-16
  • 打赏
  • 举报
回复
fread(buf, x, y, fp)

读取y个数据块,块大小为x

例如x为结构体,10个字节,那么就读取y x 10,相当于读y次,每次x字节

读y个数据,第一次读1个还是一次读10个,由x决定,相当于定义了读取单位,总字节数为x*y
goldbeef 2012-04-16
  • 打赏
  • 举报
回复
fread
Reads data from a stream.

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

Function Required Header Compatibility
fread <stdio.h> ANSI, Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged.

Parameters

buffer

Storage location for data

size

Item size in bytes

count

Maximum number of items to be read

stream

Pointer to FILE structure

Remarks

The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined.

Example

/* FREAD.C: This program opens a file named FREAD.OUT and
* writes 25 characters to the file. It then tries to open
* FREAD.OUT and read in 25 characters. If the attempt succeeds,
* the program displays the number of actual items read.
*/

#include <stdio.h>

void 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; i < 25; i++ )
list[i] = (char)('z' - i);
/* Write 25 characters to stream */
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 )
{
/* Attempt to read in 25 characters */
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" );
}


Output

Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb
酱油党 2012-04-16
  • 打赏
  • 举报
回复
返回值为读取的个数…char单位
buf存储读取出来的数据
元素的大小sizeof(char)== 1
元素的个数 3(3个char)
pfile指向FILE的指针
JackPan 2012-04-16
  • 打赏
  • 举报
回复

NAME
fread, fwrite - binary stream input/output

SYNOPSIS
#include <stdio.h>

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

DESCRIPTION
The function fread() reads nmemb elements of data, each size bytes long, from
the stream pointed to by stream, storing them at the location given by ptr.

The function fwrite() writes nmemb elements of data, each size bytes long, to
the stream pointed to by stream, obtaining them from the location given by ptr.

For non-locking counterparts, see unlocked_stdio(3).

RETURN VALUE
fread() and fwrite() return the number of items successfully read or written
(i.e., not the number of characters). If an error occurs, or the end-of-file
is reached, the return value is a short item count (or zero).
yileisen 2012-04-16
  • 打赏
  • 举报
回复
实际读取数据的字节数,当数据长度比设定的读取长度小时,实际读取的字节数要比设定值小
面包大师 2012-04-16
  • 打赏
  • 举报
回复
读取的元素的个数

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧