Header File
stdio.h
Category
Input/output Routines
Prototype
FILE *_fdopen(int handle, char *type);
FILE *_wfdopen(int handle, wchar_t *type);
Description
Associates a stream with a file handle.
_fdopen associates a stream with a file handle obtained from creat, dup, dup2, or open.
The type of stream must match the mode of the open handle.
The type string used in a call to _fdopen is one of the following values:
r |
Open for reading only. _fdopen returns NULL if the file cannot be opened. |
w |
Create for writing. If the file already exists, its contents are overwritten. |
a |
Append; open for writing at end-of-file or create for writing if the file does not exist. |
r+ |
Open an existing file for update (reading and writing). _fdopen returns NULL if the file cannot be opened. |
w+ |
Create a new file for update. If the file already exists, its contents are overwritten. |
a+ |
Open for append; open (or create if the file does not exist) for update at the end of the file. |
To specify that a given file is being opened or created in text mode, append t to the value of the type string (for example, rt or w+t).
Similarly, to specify binary mode, append b to the type string (for example, rb or w+b).
If t or b is not given in the type string, the mode is governed by the global variable _fmode.
If _fmode is set to O_BINARY, files will be opened in binary mode.
If _fmode is set to O_TEXT, files will be opened in text mode.
Note:The O_... constants are defined in fcntl.h.
When a file is opened for update, both input and output can be done on the resulting stream; however,
On successful completion _fdopen returns a pointer to the newly opened stream. In the event of error it returns NULL.
Example
#include <sys\stat.h> #include <stdio.h> #include <fcntl.h> #include <io.h> int main(void) { int handle; FILE *stream; /* open a file */ handle = open("DUMMY.FIL", O_CREAT, S_IREAD | S_IWRITE); /* now turn the handle into a stream */ stream = fdopen(handle, "w"); if (stream == NULL) printf("fdopen failed\n"); else { fprintf(stream, "Hello world\n"); fclose(stream); } return 0; }
Portability
|
POSIX |
Win32 |
ANSI C |
ANSI C++ |
_fdopen |
+ |
+ |
|
|
_wfdopen |
|
+ |
|
|
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|