fread與fread_s讀取文件(二進制文件)


fread()是c庫函數,利於移植,使用緩存,效率較read()高。

原型:

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

要注意的是它的返回值,如果讀取到了文件尾,返回值小於count,可以使用feof()函數檢測出來,返回真。

PS:返回值代表的是某種類型的size的個數。

下面程序按照1024k(一次大小為sizeof(char))一次讀取二進制文件。

include <stdio.h>
#include <string.h>


#define BUFFSIZE 1024

int main(int argc, char **argv){

    char buff[BUFFSIZE];
    FILE *fd = fopen (argv[1], "rb");
    int count, errno=0;

    bzero (buff, BUFFSIZE);
    while (!feof (fd)){
        count = fread (buff, sizeof (char), BUFFSIZE, fd);
        int n = feof (fd);
        printf ("%d,%d\n", count, n);
        printf ("%s\n",strerror (errno));
    }
    return 0;
}

fread_s讀取流的數據。 fread 此版本的具有安全增強功能,如 CRT中的安全功能所述。

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

數據的存儲位置。

bufferSize

目標緩沖區的大小(以字節為單位)。

elementSize

寫入字節的項的大小。

count

要讀取的項目最大數。

stream

為 FILE 結構的指針。

fread_s 返回讀取到緩沖區,比 count 會比(全部)的項數,如果讀取錯誤或文件結尾遇到,在 count 達到之前。 使用 feof 或 ferror 功能與一個文件關閉條件區分錯誤。 如果 size 或 count 為0,fread_s 返回0,並且緩沖區內容保持不變。 如果 stream 或 buffer 是null指針,fread_s 調用無效參數處理程序,如 參數驗證所述。 如果執行允許繼續,此功能設置 errno 到 EINVAL 並返回0。

有關錯誤代碼的更多信息,請參見 _doserrno、errno、_sys_errlist和_sys_nerr

備注
 
fread_s 函數在 buffer讀取到 elementSize 字節 count 項從輸入 stream 並存儲它們。 與 stream 的文件指針(如果有)的字節數增加實際讀取的。 如果給定流在文本模式中打開,支持返回換行符對用單個換行符替換。 替換對文件指針或返回值的效果。 ,如果發生錯誤,文件指針位置是不確定的。 一個部分讀取的項的值無法確定的。

此功能鎖定其他線程。 如果需要非固定版本,請使用 _fread_nolock。

功能

必需的標頭

fread_s

<stdio.h>

示例

// 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;
   }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM