[Previous]
[Contents]
[Next]

freopen()

reopen a stream

Synopsis:

#include <stdio.h>

FILE *freopen( const char *filename,
               const char *mode,
               FILE *fp );

Description:

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.

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. When an error has occurred, errno indicates the type of error detected.

Examples:

#include <stdio.h>

void main()
  {
    FILE *fp;
    int c;

    fp = freopen( "file", "r", stdin );
    if( fp != NULL ) {
      while( (c = fgetchar()) != EOF )
        fputchar(c);
      fclose( fp );
    }
  }

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, fclose(), fcloseall(), fdopen(), fopen(), _fsopen()


[Previous]
[Contents]
[Next]