C 中 getline (類似fgets) 的實現方法



long getline(int fd, char *buf, long len)
{
long cur = lseek(fd, 0, SEEK_CUR);
long ret = read(fd, buf, len);
int index = 0;

if(ret <= 0) {
return ret;
}


while(buf[index++] != '\n' && index < ret);

if(index < ret) {
lseek(fd, cur + index, SEEK_SET);
}

if(index < len) {
buf[index] = 0;
}
return index; 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章