Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <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() closes the file associated with 'stream' and reassigns
    'stream' to the file specified by 'pathname'.  The new file is opened
    with the access specified by 'type':

        "r"     Open for reading only.  The file must exist.

        "w"     Create a new file for writing, or open an existing file
                for writing.  Use with care: If the file already exists,
                its contents will be destroyed.

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

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

        "w+"    Open an empty file for reading and writing. Use with
                care: if the file already exists, its contents will be
                destroyed.

        "a+"    Open for reading and appending; create the file if it
                doesn't exist.  All write operations take place at the
                end of the file; existing data cannot be overwritten.

    The following characters may be appended to the above 'type' to
    specify the translation mode for the new lines:

        t       Open in text (translated) mode; The CR-LF pair is
                translated to a single LF on input; LF is translated to
                CR-LF on output.

        b       Open in binary (untranslated) mode.  The above
                translation is suppressed.

    If 't' or 'b' is not specified, the translation mode is defined by
    the default mode variable '_fmode'.  '_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.

         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).  A write can immediately follow a read which
                    returned EOF.

   -------------------------------- 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')
                   fputc(ch,stdout);
               fclose(stream);
           }


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

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