fread_sfread_s

从流读取数据。Reads data from a stream. 这是 fread 版本,具有 CRT 中的安全功能中所述的安全增强功能。This version of fread has security enhancements, as described in Security Features in the CRT.

语法Syntax

size_t fread_s(
   void *buffer,
   size_t bufferSize,
   size_t elementSize,
   size_t count,
   FILE *stream
);

参数Parameters

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

BufferSizebufferSize
目标缓冲区的大小(以字节为单位)。Size of the destination buffer in bytes.

elementSizeelementSize
要读取的项的大小(以字节为单位)。Size of the item to read in bytes.

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

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

返回值Return Value

fread_s返回的 (整数) 的项的数量已读取到缓冲区中,这可能是小于计数如果之前遇到读取的错误或文件的末尾计数为止。fread_s returns the number of (whole) items that were read into the buffer, which may be less than count if a read error or the end of the file is encountered before count is reached. 使用feofferror函数以将错误分开的文件尾条件。Use the feof or ferror function to distinguish an error from an end-of-file condition. 如果大小计数为 0, fread_s返回 0 和缓冲区内容不变。If size or count is 0, fread_s returns 0 and the buffer contents are unchanged. 如果缓冲区是 null 指针, fread_s中所述调用无效参数处理程序,参数验证.If stream or buffer is a null pointer, fread_s 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_nerrFor more information about error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注Remarks

Fread_s函数一直读取到计数的项elementSize字节从输入并将它们存储在缓冲区The fread_s function reads up to count items of elementSize bytes from the input stream and stores them in buffer. 与关联的文件指针实际读取的字节数增加 (如果有)。The file pointer that is 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 require a non-locking version, use _fread_nolock.

要求Requirements

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

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

示例Example

// crt_fread_s.c
// Command line: cl /EHsc /nologo /W4 crt_fread_s.c
//
// This program opens a file that's named FREAD.OUT and
// writes characters to the file. It then tries to open
// FREAD.OUT and read in characters by using fread_s. If the attempt succeeds,
// the program displays the number of actual items read.

#include <stdio.h>

#define BUFFERSIZE 30
#define DATASIZE 22
#define ELEMENTCOUNT 2
#define ELEMENTSIZE (DATASIZE/ELEMENTCOUNT)
#define FILENAME "FREAD.OUT"

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

   for ( i = 0; i < DATASIZE; i++ )
      list[i] = (char)('z' - i);
   list[DATASIZE] = '\0'; // terminal null so we can print it

   // Open file in text mode:
   if( fopen_s( &stream, FILENAME, "w+t" ) == 0 )
   {
      // Write DATASIZE characters to stream
      printf( "Contents of buffer before write/read:\n\t%s\n\n", list );
      numwritten = fwrite( list, sizeof( char ), DATASIZE, stream );
      printf( "Wrote %d items\n\n", numwritten );
      fclose( stream );
   } else {
      printf( "Problem opening the file\n" );
      return -1;
   }

   if( fopen_s( &stream, FILENAME, "r+t" ) == 0 )   {
      // Attempt to read in characters in 2 blocks of 11
      numread = fread_s( list, BUFFERSIZE, ELEMENTSIZE, ELEMENTCOUNT, stream );
      printf( "Number of %d-byte elements read = %d\n\n", ELEMENTSIZE, numread );
      printf( "Contents of buffer after write/read:\n\t%s\n", list );
      fclose( stream );
   } else {
      printf( "File could not be opened\n" );
      return -1;
   }
}
Contents of buffer before write/read:
        zyxwvutsrqponmlkjihgfe

Wrote 22 items

Number of 11-byte elements read = 2

Contents of buffer after write/read:
        zyxwvutsrqponmlkjihgfe

请参阅See also

流 I/OStream I/O
fwritefwrite
_read_read