freadfread

从流读取数据。Reads data from a stream.

语法Syntax

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

参数Parameters

bufferbuffer
数据的存储位置。Storage location for data.

sizesize
项目大小(以字节为单位)。Item size in bytes.

countcount
要读取的项的最大数量。Maximum number of items to be read.

stream
指向文件结构的指针。Pointer to FILE structure.

返回值Return Value

fread返回实际读取完整的项的数目,它可能小于计数如果发生错误,或如果之前已到达遇到文件末尾,则计数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. 使用feofferror函数将读取的错误的文件尾条件与区分开来。Use the feof or ferror function to distinguish a read error from an end-of-file condition. 如果大小计数为 0, fread返回 0 和缓冲区内容不变。If size or count is 0, fread returns 0 and the buffer contents are unchanged. 如果缓冲区是 null 指针, fread中所述调用无效参数处理程序,参数验证If stream or buffer is a null pointer, fread invokes the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,此函数将errnoEINVAL并返回 0。If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.

备注Remarks

Fread函数一直读取到计数的项大小字节从输入并将它们存储在缓冲区.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.

此函数将锁定其他线程。This function locks out other threads. 如果你需要的非锁定版本,使用 _fread_nolockIf you need a non-locking version, use _fread_nolock.

要求Requirements

函数Function 必需的标头Required header
freadfread <stdio.h><stdio.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_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>

int main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;

   // Open file in text mode:
   if( fopen_s( &stream, "fread.out", "w+t" ) == 0 )
   {
      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( fopen_s( &stream, "fread.out", "r+t" ) == 0 )
   {
      // 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" );
}
Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb

请参阅See also

流 I/OStream I/O
fwritefwrite
_read_read