程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 操作系統大型實驗進展(8)-----C語言函數fread

操作系統大型實驗進展(8)-----C語言函數fread

編輯:關於C語言

C語言庫函數名: 簡介   函數原型:   size_t fread(void *buffer, size_t size, size_tcount, FILE *stream);    功 能:   從一個文件流中讀數據,讀取count個元素,每個元素size字節.如果調用成功返回count.如果調用成功則實際讀取size*count字節   參 數:   buffer   用於接收數據的內存地址,大小至少是size*count 字節.   size   單個元素的大小,單位是字節   count   元素的個數,每個元素是size字節.   stream   輸入流   返回值:   實際讀取的元素數.如果返回值與count(不是count*size)不相同,則可能文件結尾或發生錯誤.   從ferror和feof獲取錯誤信息或檢測是否到達文件結尾. 程序例   #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,"Cannot open output file.\n");   return 1;   } /* write some data to the file */   fwrite(msg, 1,strlen(msg)+1, stream); /* seek to the beginning of the file */   fseek(stream, 0, SEEK_SET); /* read the data and display it */   fread(buf, 1,strlen(msg)+1,stream);   printf("%s\n", buf);   fclose(stream);   return 0;   } MSDN示例   #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" );   }  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved