Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>fdopen() open a stream using a handle</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
fdopen()                 Open a Stream Using a Handle

 #include   <stdio.h>

 FILE       *fdopen(handle,type);        Pointer to open stream
 int        handle;                      Handle referring to open file
 char       *type;                       Type of access permitted

    fdopen() associates a stream with a file 'handle' obtained from
    creat(), open(), dup(), or dup2(), thereby allowing a file opened for
    low-level I/O to be buffered and formatted.  'type' specifies the
    type of access requested for the file.  The specified 'type' must
    match the access mode with which the file was opened:

        "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 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 'types' to
    specify the translation mode for the new lines:

        t       Open in text (translated) mode; The CR-LF pair is
                translated into 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 or rewind operation when switching between reading
                and writing (or vice versa).

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

    This example opens a file in read only mode, then associates a stream
    with the open file handle:

           #include <stdio.h>
           #include <fcntl.h>

           FILE *stream;
           int fhndl;

           main()
           {
               fhndl = open("input.txt",O_RDONLY);
               .
               .
               .
               stream = fdopen(fhndl,"r");
           }

See Also: open() fopen() freopen()

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