Skip to content

Commit 5c75f26

Browse files
author
Colin Robertson
authored
Merge pull request #3924 from MicrosoftDocs/main637890102871723889
Repo sync for protected CLA branch
2 parents bef6372 + dadc3be commit 5c75f26

11 files changed

+599
-596
lines changed
Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
description: "Learn more about: _fdopen, _wfdopen"
33
title: "_fdopen, _wfdopen"
4-
ms.date: "4/2/2020"
4+
ms.date: 05/18/2022
55
api_name: ["_fdopen", "_wfdopen", "_o__fdopen", "_o__wfdopen"]
66
api_location: ["msvcrt.dll", "msvcr80.dll", "msvcr90.dll", "msvcr100.dll", "msvcr100_clr0400.dll", "msvcr110.dll", "msvcr110_clr0400.dll", "msvcr120.dll", "msvcr120_clr0400.dll", "ucrtbase.dll", "api-ms-win-crt-stdio-l1-1-0.dll", "api-ms-win-crt-math-l1-1-0.dll", "api-ms-win-crt-private-l1-1-0.dll"]
77
api_type: ["DLLExport"]
88
topic_type: ["apiref"]
9-
f1_keywords: ["_tfdopen", "_fdopen", "_wfdopen", "wfdopen", "tfdopen"]
9+
f1_keywords: ["STDIO/_fdopen", "CORECRT_WSTDIO/_wfdopen", "TCHAR/_tfdopen", "_fdopen", "_wfdopen", "_tfdopen", "wfdopen", "tfdopen"]
1010
helpviewer_keywords: ["wfdopen function", "_fdopen function", "_wfdopen function", "tfdopen function", "fdopen function", "_tfdopen function", "streams, associating with files"]
1111
ms.assetid: 262757ff-1e09-4472-a5b6-4325fc28f971
1212
---
13-
# _fdopen, _wfdopen
13+
# `_fdopen`, `_wfdopen`
1414

1515
Associates a stream with a file that was previously opened for low-level I/O.
1616

@@ -29,81 +29,81 @@ FILE *_wfdopen(
2929

3030
### Parameters
3131

32-
*fd*<br/>
32+
*`fd`*\
3333
File descriptor of the open file.
3434

35-
*mode*<br/>
35+
*`mode`*\
3636
Type of file access.
3737

38-
## Return Value
38+
## Return value
3939

40-
Each of these functions returns a pointer to the open stream. A null pointer value indicates an error. When an error occurs, the invalid parameter handler is invoked, as described in [Parameter Validation](../../c-runtime-library/parameter-validation.md). If execution is allowed to continue, **errno** is set either to **EBADF**, which indicates a bad file descriptor, or **EINVAL**, which indicates that *mode* was a null pointer.
40+
Each of these functions returns a pointer to the open stream. A null pointer value indicates an error. When an error occurs, the invalid parameter handler is invoked, as described in [Parameter validation](../../c-runtime-library/parameter-validation.md). If execution is allowed to continue, `errno` is set either to `EBADF`, which indicates a bad file descriptor, or `EINVAL`, which indicates that *`mode`* was a null pointer.
4141

42-
For more information about these and other error codes, see [_doserrno, errno, _sys_errlist, and _sys_nerr](../../c-runtime-library/errno-doserrno-sys-errlist-and-sys-nerr.md).
42+
For more information about these and other error codes, see [`_doserrno`, `errno`, `_sys_errlist`, and `_sys_nerr`](../../c-runtime-library/errno-doserrno-sys-errlist-and-sys-nerr.md).
4343

4444
## Remarks
4545

46-
The **_fdopen** function associates an I/O stream with the file that is identified by *fd*, and thus allows a file that is opened for low-level I/O to be buffered and formatted. **_wfdopen** is a wide-character version of **_fdopen**; the *mode* argument to **_wfdopen** is a wide-character string. **_wfdopen** and **_fdopen** otherwise behave identically.
46+
The **`_fdopen`** function associates an I/O stream with the file that is identified by *`fd`*, and thus allows a file that is opened for low-level I/O to be buffered and formatted. **`_wfdopen`** is a wide-character version of **`_fdopen`**; the *mode* argument to **`_wfdopen`** is a wide-character string. **`_wfdopen`** and **`_fdopen`** otherwise behave identically.
4747

48-
File descriptors passed into **_fdopen** are owned by the returned `FILE *` stream. If **_fdopen** is successful, do not call [\_close](close.md) on the file descriptor. Calling [fclose](fclose-fcloseall.md) on the returned `FILE *` also closes the file descriptor.
48+
File descriptors passed into **`_fdopen`** are owned by the returned `FILE *` stream. If **`_fdopen`** is successful, don't call [`_close`](close.md) on the file descriptor. Calling [`fclose`](fclose-fcloseall.md) on the returned `FILE *` also closes the file descriptor.
4949

50-
By default, this function's global state is scoped to the application. To change this, see [Global state in the CRT](../global-state.md).
50+
By default, this function's global state is scoped to the application. To change it, see [Global state in the CRT](../global-state.md).
5151

52-
### Generic-Text Routine Mappings
52+
The *`mode`* character string specifies the type of file access requested for the file:
5353

54-
|Tchar.h routine|\_UNICODE and \_MBCS not defined|\_MBCS defined|\_UNICODE defined|
55-
|---------------------|--------------------------------------|--------------------|-----------------------|
56-
|**_tfdopen**|**_fdopen**|**_fdopen**|**_wfdopen**|
57-
58-
The *mode* character string specifies the type of file access requested for the file:
59-
60-
| *mode* | Access |
54+
| *`mode`* | Access |
6155
|--------|--------|
62-
| **"r"** | Opens for reading. If the file does not exist or cannot be found, the **fopen** call fails. |
63-
| **"w"** | Opens an empty file for writing. If the given file exists, its contents are destroyed. |
64-
| **"a"** | Opens for writing at the end of the file (appending). Creates the file if it does not exist. |
65-
| **"r+"** | Opens for both reading and writing. The file must exist. |
66-
| **"w+"** | Opens an empty file for both reading and writing. If the file exists, its contents are destroyed. |
67-
| **"a+"** | Opens for reading and appending. Creates the file if it does not exist. |
56+
| **`"r"`** | Opens for reading. If the file doesn't exist or can't be found, the **`fopen`** call fails. |
57+
| **`"w"`** | Opens an empty file for writing. If the given file exists, its contents are destroyed. |
58+
| **`"a"`** | Opens for writing at the end of the file (appending). Creates the file if it doesn't exist. |
59+
| **`"r+"`** | Opens for both reading and writing. The file must exist. |
60+
| **`"w+"`** | Opens an empty file for both reading and writing. If the file exists, its contents are destroyed. |
61+
| **`"a+"`** | Opens for reading and appending. Creates the file if it doesn't exist. |
6862

69-
When a file is opened with the **"a"** or **"a+"** access type, all write operations occur at the end of the file. The file pointer can be repositioned by using [fseek](fseek-fseeki64.md) or [rewind](rewind.md), but it is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten. When the **"r+"**, **"w+"**, or **"a+"** access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening [fflush](fflush.md), [fsetpos](fsetpos.md), [fseek](fseek-fseeki64.md), or [rewind](rewind.md) operation. You can specify the current position for the [fsetpos](fsetpos.md) or [fseek](fseek-fseeki64.md) operation, if you want to.
63+
When a file is opened with the **`"a"`** or **`"a+"`** access type, all write operations occur at the end of the file. The file pointer can be repositioned by using [`fseek`](fseek-fseeki64.md) or [`rewind`](rewind.md), but it's always moved back to the end of the file before any write operation is carried out. Thus, existing data can't be overwritten. When the **`"r+"`**, **`"w+"`**, or **`"a+"`** access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening [`fflush`](fflush.md), [`fsetpos`](fsetpos.md), [`fseek`](fseek-fseeki64.md), or [`rewind`](rewind.md) operation. You can specify the current position for the [`fsetpos`](fsetpos.md) or [`fseek`](fseek-fseeki64.md) operation, if you want to.
7064

7165
In addition to the above values, the following characters can also be included in *mode* to specify the translation mode for newline characters:
7266

73-
| *mode* modifier | Behavior |
67+
| *`mode`* modifier | Behavior |
7468
|-----------------|----------|
75-
| **t** | Open in text (translated) mode. In this mode, carriage return-line feed (CR-LF) combinations are translated into one-line feeds (LF) on input, and LF characters are translated to CR-LF combinations on output. Also, Ctrl+Z is interpreted as an end-of-file character on input. |
76-
| **b** | Open in binary (untranslated) mode. Any translations from **t** mode are suppressed. |
77-
| **c** | Enable the commit flag for the associated *filename* so that the contents of the file buffer are written directly to disk if either **fflush** or **_flushall** is called. |
78-
| **n** | Reset the commit flag for the associated *filename* to "no-commit." This is the default. It also overrides the global commit flag if you link your program with Commode.obj. The global commit flag default is "no-commit" unless you explicitly link your program with Commode.obj. |
79-
80-
The **t**, **c**, and **n** *mode* options are Microsoft extensions for **fopen** and **_fdopen**. Do not use them if you want to preserve ANSI portability.
81-
82-
If **t** or **b** is not given in *mode*, the default translation mode is defined by the global variable [\_fmode](../../c-runtime-library/fmode.md). If **t** or **b** is prefixed to the argument, the function fails and returns NULL. For a discussion of text and binary modes, see [Text and Binary Mode File I/O](../../c-runtime-library/text-and-binary-mode-file-i-o.md).
83-
84-
Valid characters for the *mode* string used in **fopen** and **_fdopen** correspond to *oflag* arguments used in [\_open](open-wopen.md) and [\_sopen](sopen-wsopen.md), as shown in this table:
85-
86-
|Characters in *mode* string|Equivalent *oflag* value for **_open** and **_sopen**|
87-
|---------------------------------|---------------------------------------------------|
88-
|**a**|**\_O\_WRONLY &#124; \_O\_APPEND** (usually **\_O\_WRONLY &#124; \_O\_CREAT &#124; \_O\_APPEND**)|
89-
|**a+**|**\_O\_RDWR &#124; \_O\_APPEND** (usually **\_O\_RDWR &#124; \_O\_APPEND &#124; \_O\_CREAT** )|
90-
|**r**|**\_O\_RDONLY**|
91-
|**r+**|**\_O\_RDWR**|
92-
|**w**|**\_O\_WRONLY** (usually **\_O\_WRONLY &#124; \_O\_CREAT &#124; \_O\_TRUNC**)|
93-
|**w+**|**\_O\_RDWR** (usually **\_O\_RDWR &#124; \_O\_CREAT &#124; \_O\_TRUNC**)|
94-
|**b**|**\_O\_BINARY**|
95-
|**t**|**\_O\_TEXT**|
96-
|**c**|None|
97-
|**n**|None|
69+
| **`t`** | Open in text (translated) mode. In this mode, carriage return-line feed (CR-LF) combinations are translated into one-line feeds (LF) on input, and LF characters are translated to CR-LF combinations on output. Also, Ctrl+Z is interpreted as an end-of-file character on input. |
70+
| **`b`** | Open in binary (untranslated) mode. Any translations from **`t`** mode are suppressed. |
71+
| **`c`** | Enable the commit flag for the associated *filename* so that the contents of the file buffer are written directly to disk if either **`fflush`** or **`_flushall`** is called. |
72+
| **`n`** | Reset the commit flag for the associated *filename* to "no-commit." This flag is the default. It also overrides the global commit flag if you link your program with *`Commode.obj`*. The global commit flag default is "no-commit" unless you explicitly link your program with *`Commode.obj`*. |
73+
74+
The **`t`**, **`c`**, and **`n`** *`mode`* options are Microsoft extensions for **`fopen`** and **`_fdopen`**. Don't use them if you want to preserve ANSI portability.
75+
76+
If **`t`** or **`b`** isn't given in *`mode`*, the default translation mode is defined by the global variable [`_fmode`](../../c-runtime-library/fmode.md). If **`t`** or **`b`** is prefixed to the argument, the function fails and returns `NULL`. For a discussion of text and binary modes, see [Text and binary mode file I/O](../../c-runtime-library/text-and-binary-mode-file-i-o.md).
77+
78+
Valid characters for the *`mode`* string used in **`fopen`** and **`_fdopen`** correspond to *`oflag`* arguments used in [`_open`](open-wopen.md) and [`_sopen`](sopen-wsopen.md), as shown in this table:
79+
80+
| Characters in *`mode`* string | Equivalent *`oflag`* value for **`_open`** and **`_sopen`** |
81+
|--|--|
82+
| **`a`** | `_O_WRONLY | _O_APPEND` (usually `_O_WRONLY | _O_CREAT | _O_APPEND`) |
83+
| **`a+`** | `_O_RDWR | _O_APPEND` (usually `_O_RDWR | _O_APPEND | _O_CREAT`) |
84+
| **`r`** | `_O_RDONLY` |
85+
| **`r+`** | `_O_RDWR` |
86+
| **`w`** | `_O_WRONLY` (usually `_O_WRONLY | _O_CREAT | _O_TRUNC`) |
87+
| **`w+`** | `_O_RDWR` (usually `_O_RDWR | _O_CREAT | _O_TRUNC`) |
88+
| **`b`** | `_O_BINARY` |
89+
| **`t`** | `_O_TEXT` |
90+
| **`c`** | None |
91+
| **`n`** | None |
9892

9993
## Requirements
10094

101-
|Function|Required header|
102-
|--------------|---------------------|
103-
|**_fdopen**|\<stdio.h>|
104-
|**_wfdopen**|\<stdio.h> or \<wchar.h>|
95+
| Function | Required header | C++ header |
96+
|--|--|--|
97+
| **`_fdopen`** | `<stdio.h>` | `<cstdio>` |
98+
| **`_wfdopen`** | `<stdio.h>` or `<wchar.h>` | `<cstdio>` |
99+
100+
For more information on standards conformance and naming conventions in the C runtime library, see [Compatibility](../../c-runtime-library/compatibility.md).
101+
102+
### Generic-text routine mappings
105103

106-
For more compatibility information, see [Compatibility](../../c-runtime-library/compatibility.md).
104+
| `<tchar.h>` routine | `_UNICODE` and `_MBCS` not defined | `_MBCS` defined | `_UNICODE` defined |
105+
|--|--|--|--|
106+
| **`_tfdopen`** | **`_fdopen`** | **`_fdopen`** | **`_wfdopen`** |
107107

108108
## Example
109109

@@ -157,9 +157,9 @@ Lines in file: 2
157157

158158
## See also
159159

160-
[Stream I/O](../../c-runtime-library/stream-i-o.md)<br/>
161-
[\_dup, \_dup2](dup-dup2.md)<br/>
162-
[fclose, \_fcloseall](fclose-fcloseall.md)<br/>
163-
[fopen, \_wfopen](fopen-wfopen.md)<br/>
164-
[freopen, \_wfreopen](freopen-wfreopen.md)<br/>
165-
[\_open, \_wopen](open-wopen.md)<br/>
160+
[Stream I/O](../../c-runtime-library/stream-i-o.md)\
161+
[`_dup`, `_dup2`](dup-dup2.md)\
162+
[`fclose`, `_fcloseall`](fclose-fcloseall.md)\
163+
[`fopen`, `_wfopen`](fopen-wfopen.md)\
164+
[`freopen`, `_wfreopen`](freopen-wfreopen.md)\
165+
[`_open`, `_wopen`](open-wopen.md)

0 commit comments

Comments
 (0)