#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
int f1,f2;int n=0;
char p[34];
int h=0;
f1=dup(0);//open(0,O_RDWR|O_CREAT|O_NONBLOCK,0666);
while(h<10)
{
n=read(f1, p+h, 34);
if(n==-1)
{
if(errno==EAGAIN)
{
continue;
}else
{
perror("error!");
break;
}
}else
{
h+=n;
}
}
printf("%d-%s\n",n,p);
close(f1);
return 0;
}
输入10个字符以上,再按回车
本文详细介绍了如何使用C++标准库中的`unistd.h`, `stdio.h`, `fcntl.h`, `errno.h`等头文件来实现高效率地从文件中读取数据,并在遇到错误如EAGAIN时进行智能处理。通过循环读取和错误检查,确保了程序的稳定性和鲁棒性。
1598

被折叠的 条评论
为什么被折叠?



