home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / fgets.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  97 lines

  1. /***
  2. *fgets.c - get string from a file
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines fgets() - read a string from a file
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <dbgint.h>
  14. #include <file2.h>
  15. #include <internal.h>
  16. #include <mtdll.h>
  17. #include <tchar.h>
  18.  
  19. /***
  20. *char *fgets(string, count, stream) - input string from a stream
  21. *
  22. *Purpose:
  23. *       get a string, up to count-1 chars or '\n', whichever comes first,
  24. *       append '\0' and put the whole thing into string. the '\n' IS included
  25. *       in the string. if count<=1 no input is requested. if EOF is found
  26. *       immediately, return NULL. if EOF found after chars read, let EOF
  27. *       finish the string as '\n' would.
  28. *
  29. *Entry:
  30. *       char *string - pointer to place to store string
  31. *       int count - max characters to place at string (include \0)
  32. *       FILE *stream - stream to read from
  33. *
  34. *Exit:
  35. *       returns string with text read from file in it.
  36. *       if count <= 0 return NULL
  37. *       if count == 1 put null string in string
  38. *       returns NULL if error or end-of-file found immediately
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43.  
  44. #ifdef _UNICODE
  45. wchar_t * __cdecl fgetws (
  46. #else  /* _UNICODE */
  47. char * __cdecl fgets (
  48. #endif  /* _UNICODE */
  49.         _TSCHAR *string,
  50.         int count,
  51.         FILE *str
  52.         )
  53. {
  54.         REG1 FILE *stream;
  55.         REG2 _TSCHAR *pointer = string;
  56.         _TSCHAR *retval = string;
  57.         int ch;
  58.  
  59.         _ASSERTE(string != NULL);
  60.         _ASSERTE(str != NULL);
  61.  
  62.         if (count <= 0)
  63.                 return(NULL);
  64.  
  65.         /* Init stream pointer */
  66.         stream = str;
  67.  
  68.         _lock_str(stream);
  69.  
  70.         while (--count)
  71.         {
  72. #ifdef _UNICODE
  73.                 if ((ch = _getwc_lk(stream)) == WEOF)
  74. #else  /* _UNICODE */
  75.                 if ((ch = _getc_lk(stream)) == EOF)
  76. #endif  /* _UNICODE */
  77.                 {
  78.                         if (pointer == string) {
  79.                                 retval=NULL;
  80.                                 goto done;
  81.                         }
  82.  
  83.                         break;
  84.                 }
  85.  
  86.                 if ((*pointer++ = (_TSCHAR)ch) == _T('\n'))
  87.                         break;
  88.         }
  89.  
  90.         *pointer = _T('\0');
  91.  
  92. /* Common return */
  93. done:
  94.         _unlock_str(stream);
  95.         return(retval);
  96. }
  97.