home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / CLIBSRC1.ZIP / FGETS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  1.5 KB  |  51 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - fgets.c
  3.  *
  4.  * function(s)
  5.  *        fgets - gets a string from a stream
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <stdio.h>
  18.  
  19. /*---------------------------------------------------------------------*
  20.  
  21. Name            fgets - gets a string from a stream
  22.  
  23. Usage           char *fgets(char *string, int n, FILE *stream);
  24.  
  25. Prototype in    stdio.h
  26.  
  27. Description     reads characters from stream into the string string:
  28.                 The function stops reading when it either reads n-1
  29.                 characters or reads a newline character (whichever
  30.                 comes first).  fgets retains the newline character.
  31.                 The last character read into string is followed by a
  32.                 null character.
  33.  
  34. Return value    success : pointer to string
  35.                 failure : NULL
  36.  
  37. *---------------------------------------------------------------------*/
  38. char * _FARFUNC fgets (char *s, int n, FILE *fp)
  39. {
  40.         register    int    c = 0;
  41.         register    char    *P;
  42.  
  43.         P = s;
  44.  
  45.         while ('\n' != c && --n > 0  &&  (c = getc(fp)) != EOF)  *P++ = c;
  46.  
  47.         if (EOF == c && P == s)  return( NULL );
  48.         *P = 0;
  49.         return (ferror (fp)) ? NULL : s;
  50. }
  51.