fread

fread

fread是一個函式,它從檔案流中讀數據,函式原型為:size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;從給定流 stream 讀取數據,最多讀取count個項,每個項size個位元組,如果調用成功返回實際讀取到的項個數(小於或等於count),如果不成功或讀到檔案末尾返回 0。

基本介紹

  • 中文名:fread
  • 外文名:fread
  • 屬性:函式
  • 參數:buffer size,count,stream
  • 功能:數據讀取
  • 所屬庫:#include <stdio.h>
fread函式簡介,函式原型,參 數,返回值,程式例,C語言,MSDN示例,PHP,PHP函式,說明,注意,MATLAB函式,功能:,語法:,例子:,

fread函式簡介

函式原型

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

參 數

buffer
用於接收數據的記憶體地址
size
要讀的每個數據項的位元組數,單位是位元組
count
要讀count個數據項,每個數據項size個位元組.
stream
輸入流

返回值

返回真實讀取的項數,若大於count則意味著產生了錯誤。另外,產生錯誤後,檔案位置指示器是無法確定的。若其他stream或buffer為空指針,或在unicode模式中寫入的位元組數為奇數,此函式設定errno為EINVAL以及返回0.

程式例

C語言

#include<stdio.h>#include<string.h>int main(void){    FILE*stream;    char msg[]="this is a test";    char buf[20];    if((stream=fopen("DUMMY.FIL","w+"))==NULL)    {        fprintf(stderr,"Can not open output file.\n");        return 0;    }    /*write some data to the file*/    fwrite(msg,1,strlen(msg)+1,stream);    /*sizeof(char)=1 seek to the beginning of the file*/    fseek(stream,0,SEEK_SET);    /*read the data and display it*/    fread(buf,strlen(msg)+1,1,stream);
    printf("%s\n",buf);    fclose(stream);    return 0;}

MSDN示例

#include<stdio.h>int main(void){    FILE*stream;    charlist[30];    inti,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");}

PHP

<?php    $handle=fopen("test.txt","rb");    $contents="";    while(!feof($handle)){        $contents.=fread($handle,8192);    }    fclose($handle);?>

PHP函式

(PHP 4, PHP 5)
fread -- 讀取檔案(可安全用於二進制檔案)

說明

stringfread( int handle, int length )
fread()檔案指針handle讀取最多 length 個位元組。 該函式在讀取完 length 個位元組數,或到達 EOF 的時候,或(對於網路流)當一個包可用時就會停止讀取檔案,視乎先碰到哪種情況。

注意

在區分二進制檔案和文本檔案的系統上(如 Windows)打開檔案時,fopen() 函式的 mode 參數要加上 'b'。
當從網路流或者管道讀取時,例如在讀取從遠程檔案或popen()以及proc_open()的返回時,讀取會在一個包可用之後停止。這意味著你應該如下例所示將數據收集起來合併成大塊。
如果你只是想將一個檔案的內容讀入到一個字元串中,用file_get_contents(),它的性能比上面的代碼好得多。

MATLAB函式

功能:

fread函式可從檔案中讀取二進制數據

語法:

A = fread(fid, count)
A = fread(fid, count, precision)
其中fid為指針所指檔案中的當前位置,count指讀取的數據個數, precision表示以什麼格式的數據類型讀取數據。

例子:

fid = fopen('alphabet.txt', 'r');
c = fread(fid, 5)'
c =
65 66 67 68 69
fclose(fid);
程式說明:alphabet檔案中按順序存儲著26個英文字母,讀取檔案之前要先打開檔案,由於未指定讀取數據的類型,所以程式指定默認類型為無符號字元型即uchar,65、66、67、68、69代表A、B、C、D、E,檔案用完還要記得關閉檔案,以便釋放指針。
fid = fopen('alphabet.txt', 'r');
c = fread(fid, '*char')'
c =
ABCDEFGHIJKLMNOPQRSTUVWXYZ
fclose(fid);
程式說明:此程式未指定數據個數,而默認為全部數據,所以顯示了檔案中的所有數據。

相關詞條

熱門詞條

聯絡我們