日期:2014-05-16 浏览次数:20759 次
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
char buf[20] = {0};
int fd1;
int num;
fd1 = open("test.txt", O_RDONLY, 0);
while((num = read(fd1, buf, 19)) > 0)
{
buf[19] = '\0';
printf("num:%d,buf:%s\n",num,buf);
}
if(num <0)
printf("read error\n");
else if(num == 0)
printf("read eof of file\n ");
else//读取数据不足 指定数据19时
{
printf("num:%d,buf:%s\n",num,buf);
num = read(fd1, buf, 19);//再度一次,确定是否读取完毕
if(num == 0)
printf("read eof of file\n ");
else
printf("read error\n");
}
return 0;
}