Linux:C語言編程:fread

來源:互聯網
上載者:User

fread

函數名: fread
功  能: 從一個流中讀資料
用  法: int fread(void *ptr, int size, int nitems, FILE *stream);
參  數:用於接收資料的地址(ptr)
        單個元素的大小(size)
        元素個數(nitems)
        提供資料的檔案指標(stream)
傳回值:成功讀取的元素個數
程式例:
#include <string.h>
#include <stdio.h>
int main(void)
{
   FILE *stream;
   char msg[] = "this is a test";
   char buf[20];
   if ((stream = fopen("DUMMY.FIL", "w+"))
       == NULL)
   {
      fprintf(stderr,
              "Cannot open output file.\n");
      return 1;
   }
   /* write some data to the file */
   fwrite(msg, strlen(msg)+1, 1, stream);
   /* seek to the beginning of the file */
   fseek(stream, SEEK_SET, 0);
   /* read the data and display it */
   fread(buf, 1, strlen(msg)+1, stream);
   printf("%s\n", buf);
   fclose(stream);
   return 0;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.