标准io与文件io中的标准输入、标准输出、标准出错:
文件io回显函数
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define N 32
int main(int argc, const char *argv[])
{
int ret;
char buf[N];
while( (ret = read(0, buf, N)) > 0)
{
if(strncmp(buf, "quit", 4) == 0)
{
exit(1);
}
printf("---->");
fflush(stdout);//刷新标准输出缓存区,打印"---->",否则由于行缓存没有达到刷新的条件‘\n’、写满、正常退出,
//打印不出来。注意这个函数的参数类型是FILE *stream;
write(1, buf, ret);
}
return 0;
}
编译执行如下图:
标准io回显函数
#include <stdio.h>
#define N 32
int main(int argc, const char *argv[])
{
char buf[N];
while(fgets(buf, N, stdin) != NULL)
{
if(strncmp(buf, "quit", 4) == 0)
{
exit(1);
}
printf("--->");
fputs(buf, stdout);
}
return 0;
}
编译运行如下图: