首頁>Club>
32
回覆列表
  • 1 # a山下的人

    函式原型定義:

    ssize_t read[1] (int fd, void *buf, size_t count);

    2/8

    使用的標頭檔案:

    #include <unistd.h>

    3/8

    函式的返回值說明:

    (1)如果成功,返回讀取的位元組數;

    (2)如果出錯,返回-1並設定errno;

    (3)如果在調read函式之前已是檔案末尾,則返回0

    4/8

    函式的引數【int fd】:

    這個是檔案指標

    5/8

    函式的引數【void *buf】:

    讀上來的資料儲存在緩衝區buf中,同時檔案的當前讀寫位置向後移

    6/8

    函式的引數【size_t count】:

    是請求讀取的位元組數。若引數count 為0, 則read()不會有作用並返回0. 返回值為實際讀取到的位元組數, 如果返回0

    7/8

    函式的使用注意事項:

    如果順利 read()會返回實際讀到的位元組數, 最好能將返回值與引數count 作比較, 若返回的位元組數比要求讀取的位元組數少, 則有可能讀到了檔案尾

    8/8

    read()函式使用的簡單例子:#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>main(){ int fd, size; char s[] = "Linux Programmer!\n", buffer[80]; fd = open("/tmp/temp", O_WRONLY|O_CREAT); write(fd, s, sizeof(s)); close(fd); fd = open("/tmp/temp", O_RDONLY); size = read(fd, buffer, sizeof(buffer)); close(fd); printf("%s", buffer);}

  • 中秋節和大豐收的關聯?
  • 為什麼義和團出現在清朝皇族最腐敗的時代?