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

  1. /***
  2. *freopen.c - close a stream and assign it to a new file
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines freopen() - close and reopen file, typically used to redirect
  8. *       stdin/out/err/prn/aux.
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <stdio.h>
  14. #include <file2.h>
  15. #include <share.h>
  16. #include <dbgint.h>
  17. #include <internal.h>
  18. #include <mtdll.h>
  19. #include <tchar.h>
  20.  
  21. /***
  22. *FILE *freopen(filename, mode, stream) - reopen stream as new file
  23. *
  24. *Purpose:
  25. *       Closes the file associated with stream and assigns stream to a new
  26. *       file with current mode.  Usually used to redirect a standard file
  27. *       handle.
  28. *
  29. *Entry:
  30. *       char *filename - new file to open
  31. *       char *mode - new file mode, as in fopen()
  32. *       FILE *stream - stream to close and reassign
  33. *
  34. *Exit:
  35. *       returns stream if successful
  36. *       return NULL if fails
  37. *
  38. *Exceptions:
  39. *
  40. *******************************************************************************/
  41.  
  42. FILE * __cdecl _tfreopen (
  43.         const _TSCHAR *filename,
  44.         const _TSCHAR *mode,
  45.         FILE *str
  46.         )
  47. {
  48.         REG1 FILE *stream;
  49.         FILE *retval;
  50.  
  51.         _ASSERTE(filename != NULL);
  52.         _ASSERTE(*filename != _T('\0'));
  53.         _ASSERTE(mode != NULL);
  54.         _ASSERTE(str != NULL);
  55.  
  56.         /* Init stream pointer */
  57.         stream = str;
  58.  
  59.         _lock_str(stream);
  60.  
  61.         /* If the stream is in use, try to close it. Ignore possible
  62.          * error (ANSI 4.9.5.4). */
  63.         if ( inuse(stream) )
  64.                 _fclose_lk(stream);
  65.  
  66.         stream->_ptr = stream->_base = NULL;
  67.         stream->_cnt = stream->_flag = 0;
  68. #ifdef _UNICODE
  69.         retval = _wopenfile(filename,mode,_SH_DENYNO,stream);
  70. #else  /* _UNICODE */
  71.         retval = _openfile(filename,mode,_SH_DENYNO,stream);
  72. #endif  /* _UNICODE */
  73.  
  74.         _unlock_str(stream);
  75.         return(retval);
  76. }
  77.