'programming/C_C++'에 해당되는 글 279건

  1. 2012.10.23 readline

readline

programming/C_C++ 2012. 10. 23. 13:13
반응형

 

int readline(int fd, char *ptr, int maxlen)
{
 int n, rc;
 char c;

 for (n = 1; n < maxlen; n++) {

  if ((rc = read(fd, &c, 1)) == 1) {

   *ptr++ = c;

   if (c == '\n') {

    break;

   }

  }
  else if (rc == 0) {

   if (n == 1) {

    return (0);  // EOF, no data read

   }
   else {

    break;   // EOF, some data was read

   }

  }
  else {

   return (-1);  // error

  }

 }

  *ptr = 0;

  return (n);

}

 

 

 

반응형
Posted by 공간사랑
,