freopen

Opens a file and associates a stream with it

Local Servers:blocking
Remote Servers:blocking
Classification:ANSI
Platform:NLM
Service:Stream I/O

Syntax

  #include <stdio.h> 
    
  FILE *freopen  (  
     const char   *filename,   
     const char   *mode,   
     FILE         *fp);
  

Parameters

filename
(IN) Points to the name of the file to be opened.
mode
(IN) Points to the file mode.
fp
(IN) Points to the file structure.

Return Values

The freopen function returns a pointer to the object controlling the stream. This pointer must be passed as a parameter to subsequent functions for performing operations on the file. If the open operation fails, freopen returns NULL. If an error occurs, errno is set.

Remarks

The stream located by the fp pointer is closed. The freopen function opens the file whose name is the string pointed to by filename, and associates a stream with it. The stream information is placed in the structure located by the fp pointer.

The argument mode is described in the description of the fopen function.

NOTE:For an example of how to reverse the effect of redirecting stdin, see the example for fdopen.

See Also

fdopen, fopen

freopen Example

  #include <stdio.h>  
  main ()  
   
  {  
     FILE   *fp;  
     fp = freopen ("report.dat", "r", stdin);  
  }