이런 일은 왜 발생하는건가요?

Seven..의 이미지

예를들어 /a.txt가 있을때

int fd, filelength, remainbyte, readbyte;

fd = open("/a.txt", _O_RDONLY);
lseek(fd, 0l, SEEK_END);
filelength = tell(fd);
remainbyte = filelength;
lseek(fd, 0l, SEEK_SET);

while(remainbyte>0)
{
     readbyte = read(fd, Buf, SIZE); // char Buf[SIZE]가 있다고 하고
     remainbyte -= readbyte;
}

이렇게 할 경우..
파일의 어느정도를 읽다가 멈춰 버립니다..
정확히는 무한루틴을 도는군요;;
분명 읽어올 것이 남았다고 사료 되는데도불구하고..
계속해서.... read가 0이 들어오는..이유는 무엇일까요?
youngminny의 이미지

만약에 read()가 -1을 error로 리턴하면 위 문장은 어떻게 될까요??

서지훈의 이미지

Quote:
RETURN VALUES
If successful, the number of bytes actually read is returned. Upon read-
ing end-of-file, zero is returned. Otherwise, a -1 is returned and the
global variable errno is set to indicate the error.

read()의 확일한 에러 처리를 해주어야 이런 현상이 없겠죠.
단순한 테스트용이 아니라 실제 코드에 이런 코드가 들어간다면 시스템 다운에 큰 일조를 할것 같군요.-_-ㅋ

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);