Format
#include <io.h> int _open_osfhandle(long h, int flag);
Language Level: Extension
_open_osfhandle maps the API handle into the corresponding
run-time handle as defined by h,
and returns it.
The oflag is an integer expression formed by combining one or more of the following constants, defined in <fcntl.h>. To specify more than one constant, join the constants with the bitwise OR operator (|); for example, O_CREAT | O_TEXT.
| Oflag | Meaning |
| O_APPEND | Reposition the file pointer to the end of the file before every write operation. |
| O_RDONLY | Open the file for reading only. If this flag is given, neither O_RDWR nor O_WRONLY can be given. |
| O_RDWR | Open the file for reading and writing. If this flag is given, neither O_RDONLY nor O_WRONLY can be given. |
| O_WRONLY | Open the file for writing only. If this flag is given, neither O_RDONLY nor O_RDWR can be given. |
| O_BINARY | Open the file in binary (untranslated) mode. |
| O_TEXT | Open the file in text (translated) mode. |
If neither O_BINARY or O_TEXT is specified, the default will be O_TEXT; it is an error to specify both O_BINARY and O_TEXT. You must specify one of the access mode flags, O_RDONLY, O_WRONLY, or O_RDWR. There is no default.
Return Value
If a run-time handle did map to the passed API handle,
_open_osfhandle returns that handle and the oflag argument is
ignored. If such a run-time handle does not exist in the run-time
environment, _open_osfhandle will create a new handle as defined
by oflag.
Example
This example determines the API handle for standard output by
calling GetStdHandle, and then maps it into the C run-time handle
with _open_osfhandle.
#define WIN32LEAN_AND_MEAN #include <windows.h> #include <fcntl.h> #include <io.h> #include <string.h>
char s[] = "Hello";
int main(void) {
int h;
h = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), O_WRONLY|O_TEXT);
write(h, s, strlen(s)); return 0; }
![]()
_get_osfhandle
open -- Open File
_sopen -- Open Shared File
creat -- Create New File
fileno -- Determine File Handle
<fcntl.h>
#include