使用標準輸入&&標準輸出,進行文件複製
使用Linux/Unix的不帶緩衝區得I/0函數read 和 write實現使用標準輸入&&標準輸出,進行文件複製。
代碼如下:
#include <iostream>
#include "apue.h"
#include "err_msg.h"
using namespace std;
#define BUFSIZE 4096
int main()
{
int n;
char buf[BUFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFSIZE)) > 0)
{
if (write(STDOUT_FILENO, buf, n) != n)
{
err_sys("write error");
}
}
if (n < 0)
{
err_sys("read error");
}
return 0;
}