程序清單5-1 用getc和putc將標準輸入復制到標準輸出
#include "apue.h"intmain( void ){ int c; while(( c = getc( stdin )) != EOF ) if( putc( c, stdout ) == EOF ) err_sys( "output error" ); if( ferror( stdin )) err_sys( "input error" ); exit( 0 );}
程序清單5-2 用fgets和fputs將標準輸入復制到標準輸出
#include "apue.h"intmain( void ){ char buf[MAXLINE]; while( fgets( buf, MAXLINE, stdin ) != NULL ) if( fputs( buf, stdout ) == EOF ) err_sys( "output error" ); if( ferror( stdin )) err_sys( "input error" ); exit( 0 );}
系統調用與普通的函數調用相比通常需要花費更多的時間。
結合《文件I/O之I/O的效率》篇,我們了解到的基本事實是:標準I/O庫與直接調用read和write函數相比并不慢很多。
對于大多數比較復雜的應用程序,最主要的用戶CPU時間是由應用本身的各種處理消耗的,而不是由標準I/O例程消耗的。
本篇博文內容摘自《UNIX環境高級編程》(第二版),僅作個人學習記錄所用。關于本書可參考:http://www.apuebook.com/。
新聞熱點
疑難解答