1.函數
函數open、read、write、lseek以及close提供了不帶緩衝的I/O。這些函數都使用文件描述符。
2.標準輸入讀,標準輸出寫,代碼如下:
1 #include <stdio.h>
2 #include <unistd.h>
3
4 #define BUFFSIZE 4096
5
6 int main(void)
7 {
8 int n;
9 char buf[BUFFSIZE];
10
11 while((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
12 if(write(STDOUT_FILENO, buf, n) != n)
13 printf("write error\n");
14
15 if(n < 0)
16 printf("read error\n");
17
18 return 0;
19 }
3.編譯
gcc fig1-4.c -o fig1-4
4.運行及結果
輸入:標準輸入;輸出:標準輸出:
輸入:標準輸入;輸出:輸出至文件:
輸入:文件輸入;輸出:標準輸出:
輸入:文件輸入;輸出:文件輸出:
5.最後
輸入輸出文件採用了輸入輸出重定向。
< 將後面跟隨的文件內容作爲輸入讀入;
> 將輸出結果寫入後面跟隨的文件中;