home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / croutes.zip / GETLINE.C < prev    next >
Text File  |  1980-01-01  |  1KB  |  45 lines

  1. /*                       *** getline.c ***                           */
  2. /*                                                                   */
  3. /* IBM - PC microsoft "C"                                            */
  4. /*                                                                   */
  5. /* Function to read a record from a file into a string.  Returns the */
  6. /* length of the string including the NULL, an EOF if EOF is reached,*/
  7. /* or a -1 if an error occured.                                      */
  8. /*                                                                   */
  9. /* Written by L. Cuthbertson, March 1984.                            */
  10. /*                                                                   */
  11. /*********************************************************************/
  12. /*                                                                   */
  13.  
  14. #include <stdio.h>
  15.  
  16. int getline(fp,line,len)
  17. char line[];
  18. int len;
  19. FILE *fp;
  20. {
  21.     int c,inlen;
  22.  
  23.     /* read character at a time */
  24.     for(inlen=0;(((c=getc(fp)) != '\r') && (c != '\n'));inlen++) {
  25.  
  26.         /* error checking */
  27.         if (inlen >= len) return (-1);
  28.  
  29.         /* EOF check */
  30.         if (c == EOF) {
  31.             line[inlen] = NULL;
  32.             return(EOF);
  33.         }
  34.  
  35.         /* move character into line */
  36.         line[inlen] = c;
  37.     }
  38.  
  39.     /* replace CR or NL with a NULL */
  40.     line[inlen] = NULL;
  41.  
  42.     /* done */
  43.     return(inlen+1);
  44. }
  45.