"); //-->
代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
int fd=-1,i;
ssize_t size=-1;
char buf[10];
char filename[]="test.txt";
fd=open(filename,O_RDONLY);
if(-1==fd)
{
printf("OPen file %s failure,fd:%d\n",filename,fd);
}
else
{
printf("OPen file %s success,fd:%d\n",filename,fd);
}
while(size)
{
size=read(fd,buf,10);
if(-1==size)
{
close(fd);
printf("read file error occurs\n");
return -1;
}
else
{
if(size>0)
{
printf("read %d bytes:",size);
printf("\"");
for(i=0;i<size;i++)
{
printf("%c",*(buf+i));
}
printf("\"\n");
}
else
{
printf("reach the end of file\n");
}
}
}
return 0;
}test.txt
quick brown fox jumps over the lazy dog
编译运行


*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。