Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>freopen() reassign a file pointer</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 freopen()               Reassign a File Pointer

 #include   <stdio.h>

 FILE       *freopen(pathname,type,stream);
 const char *pathname;                   Pathname of new file
 const char *type;                       Type of access permitted
 FILE       *stream;                     Pointer to FILE structure

    freopen() reassigns 'stream' to the file specified by 'pathname'.
    The file formerly associated with 'stream' is closed, and the new
    file is opened with the access specified by 'type':

        "r"     Read access; the file must already exist.

        "w"     Write access.  Creates a new file for writing or opens an
                existing file for writing.  If the file already exists,
                its contents will be destroyed, so use with caution.

        "a"     Append.  Opens a file for writing at the end-of-file, or
                creates a file for writing if the file doesn't exist.
                Existing data cannot be overwritten in this mode.

        "r+"    Update. Opens an existing file for reading and writing;
                the file must exist.

        "w+"    Read and write acces.  Opens an empty file for reading
                and writing.  If the file exists, its contents will be
                destroyed, so use with caution.

        "a+"    Read and append.  Opens a file for reading and appending.
                If the file doesn't exist, it is created.  All write
                operations take place at the end of the file; existing
                data cannot be overwritten.

    To specify translation mode for new lines, append either of the
    following characters to 'type':

        t       text, or translated mode: CR-LF pairs are translated into
                LF on input; LF is translated to CR-LF on output.

        b       binary, or untranslated mode; translation of CR-LF to LF
                and LF to CR-LF is suppressed.

    If 't' or 'b' is not specified, the default mode variable '_fmode'
    defines the translation mode.  '_fmode' can be set to O_BINARY,
    binary mode, or O_TEXT, text mode.  These constants are defined in
    <fcntl.h>.

    Returns:    A pointer to the open stream.  NULL is returned on error,
                and the original file is closed.

      Notes:    Both reading and writing are allowed when using "r+",
                "w+" or "a+".  However, you must have an intervening
                fseek, fsetpos, or rewind operation when switching
                between reading and writing (or vice versa).

 Portability:   The 'type' parameter 't' is a Microsoft extension, and
                should not be used where ANSI support is required.

   -------------------------------- Example ---------------------------------

    The following statements reassign 'stdout' to "data2".

           #include <stdio.h>

           FILE *stream;
           char ch;

           main()
           {
               stream = freopen("data2","w+",stdout);
                   while ((ch = fgetchar()) != '\n')
                          fputchar(ch);
               fclose(stream);
           }



See Also: fclose() fileno() fopen() open()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson