使用标準輸入和輸出,fd是0 和1 的,因為linux系統shell 提供 i/o重定向是以
很多程式使用的都是标準輸入和輸出,這樣的話會很友善,并且不用自己關閉
因為程序結束的時候,os會自動幫你關閉是以打開的檔案。
使用 :
編輯的話:
gcc copyfile.c -o t
./t < copyfile.c > temp /* 複制檔案内容到 temp */
./t < copyfile.c /* 輸出到shell */
#include <unistd.h>
#define BUFFERSIZE 4096
int main(void) {
int n;
char buf[BUFFERSIZE];
while ((n = read(, buf, BUFFERSIZE)) > )
if (write(, buf, n) != n )
write(,"write error!\n",);
if (n < )
write(,"read error!\n",);
}