user-0 提问: 送分了，好简单的问题，fread()

回复:
user: user-1
content: 在某些情况下是等价的

user: user-2
content: MSDN上说得已经很清楚了： 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   Stream I/O Routines  See Also   fwrite, _read

user: user-3
content: 虽然看起来差不多，但函数对size和count的处理方式是不一样的，这就涉及到效率问题。最好按照函数的本意去使用。

user: user-0
content: 引用 1 楼 Saleayas 的回复:不一样的。 试试 如果只有 1023 个？？  如果大于1024字节的文件呢，比如 20480 ， 你觉得调用一次效果应该是一样吗？

user: user-0
content: 按道理来说： 读1次最多1024字节 读1024次1字节 含义有区别？

user: user-4
content: 不一样的。  试试 如果只有 1023 个？？

user: user-5
content: size 要读的每个数据项的字节数，单位是字节 count 要读count个数据项，每个数据项size个字节.