home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / FGETS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  1.8 KB  |  52 lines

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