Skip to content

Commit a83b489

Browse files
Merge pull request #4532 from MicrosoftDocs/main638185591780129962sync_temp
For protected CLA branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents 6a8f9b8 + d285f6e commit a83b489

File tree

6 files changed

+88
-74
lines changed

6 files changed

+88
-74
lines changed

docs/c-runtime-library/reference/fopen-s-wfopen-s.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "fopen_s, _wfopen_s"
33
description: "Describes the API for `fopen_s` and `_wfopen_s`"
4-
ms.date: 05/18/2022
4+
ms.date: 04/27/2023
55
api_name: ["_wfopen_s", "fopen_s", "_o__wfopen_s", "_o_fopen_s"]
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"]
77
api_type: ["DLLExport"]
@@ -31,10 +31,10 @@ errno_t _wfopen_s(
3131
### Parameters
3232

3333
*`pFile`*\
34-
A pointer to the file pointer that will receive the pointer to the opened file.
34+
A pointer to the file pointer that receives the pointer to the opened file.
3535

3636
*`filename`*\
37-
Filename.
37+
The name of the file to open.
3838

3939
*`mode`*\
4040
Type of access permitted.
@@ -55,7 +55,7 @@ Zero if successful; an error code on failure. For more information about these e
5555

5656
The **`fopen_s`** and **`_wfopen_s`** functions can't open a file for sharing. If you need to share the file, use [`_fsopen` or `_wfsopen`](fsopen-wfsopen.md) with the appropriate sharing mode constant—for example, use `_SH_DENYNO` for read/write sharing.
5757

58-
The **`fopen_s`** function opens the file that's specified by *`filename`*. **`_wfopen_s`** is a wide-character version of **`fopen_s`**; the arguments to **`_wfopen_s`** are wide-character strings. **`_wfopen_s`** and **`fopen_s`** behave identically otherwise.
58+
The **`fopen_s`** function opens the file specified by *`filename`*. **`_wfopen_s`** is a wide-character version of **`fopen_s`** and the arguments to **`_wfopen_s`** are wide-character strings. **`_wfopen_s`** and **`fopen_s`** behave identically, otherwise.
5959

6060
**`fopen_s`** accepts paths that are valid on the file system at the point of execution; UNC paths and paths that involve mapped network drives are accepted by **`fopen_s`** as long as the system that's executing the code has access to the share or mapped network drive at the time of execution. When you construct paths for **`fopen_s`**, don't make assumptions about the availability of drives, paths, or network shares in the execution environment. You can use either forward slashes (/) or backslashes (\\) as the directory separators in a path.
6161

@@ -111,14 +111,14 @@ When the **`"r+"`**, **`"w+"`**, or **`"a+"`** access type is specified, both re
111111

112112
Starting in C11, you can append **`"x"`** to **`"w"`** or **`"w+"`** to cause the function fail if the file exists, instead of overwriting it.
113113

114-
In addition to the values above, the following characters can be included in *`mode`* to specify the translation mode for newline characters:
114+
In addition to the previous values, the following characters can be included in *`mode`* to specify the translation mode for newline characters:
115115

116116
| *`mode`* modifier | Translation mode |
117117
|--|--|
118-
| **`t`** | Open in text (translated) mode. |
118+
| **`t`** | Open in text (translated) mode. Carriage return-line feed (CR-LF) combinations are translated into single line feeds (LF) on input and LF characters are translated to CR-LF combinations on output. CTRL+Z is interpreted as an end-of-file character on input. |
119119
| **`b`** | Open in binary (untranslated) mode; translations involving carriage-return and line feed characters are suppressed. |
120120

121-
In text (translated) mode, `CTRL`+**Z** is interpreted as an end-of-file character on input. In files opened for reading/writing with **`"a+"`**, **`fopen_s`** checks for a `CTRL`+**Z** at the end of the file and removes it, if possible. It's removed because using [`fseek`](fseek-fseeki64.md) and [`ftell`](ftell-ftelli64.md) to move within a file that ends with a `CTRL`+**Z**, may cause **`fseek`** to behave improperly near the end of the file.
121+
In text (translated) mode, `CTRL`+**Z** is interpreted as an end-of-file character on input. For files opened for reading/writing with **`"a+"`**, **`fopen_s`** checks for a `CTRL`+**Z** at the end of the file and removes it, if possible. It's removed because using [`fseek`](fseek-fseeki64.md) and [`ftell`](ftell-ftelli64.md) to move within a file that ends with a `CTRL`+**Z**, may cause **`fseek`** to behave improperly near the end of the file.
122122

123123
Also, in text mode, carriage return/line feed (CRLF) combinations are translated into single line feed (LF) characters on input, and LF characters are translated to CRLF combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. The Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the **`mbtowc`** function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the **`wctomb`** function).
124124

@@ -133,8 +133,8 @@ For more information about using text and binary modes in Unicode and multibyte
133133
| **`N`** | Specifies that the file isn't inherited by child processes. |
134134
| **`S`** | Specifies that caching is optimized for, but not restricted to, sequential access from disk. |
135135
| **`R`** | Specifies that caching is optimized for, but not restricted to, random access from disk. |
136-
| **`t`** | Specifies a file as temporary. If possible, it isn't flushed to disk. |
137-
| **`D`** | Specifies a file as temporary. It's deleted when the last file pointer is closed. |
136+
| **`T`** | Specifies a file that isn't written to disk unless memory pressure requires it. |
137+
| **`D`** | Specifies a temporary file that is deleted when the last file pointer to it is closed. |
138138
| **`ccs=UNICODE`** | Specifies UNICODE as the encoded character set to use for this file. Leave unspecified if you want ANSI encoding. |
139139
| **`ccs=UTF-8`** | Specifies UTF-8 as the encoded character set to use for this file. Leave unspecified if you want ANSI encoding. |
140140
| **`ccs=UTF-16LE`** | Specifies UTF-16LE as the encoded character set to use for this file. Leave unspecified if you want ANSI encoding. |
@@ -150,21 +150,26 @@ Valid characters for the *`mode`* string used in **`fopen_s`** and [`_fdopen`](f
150150
| **`w`** | `_O_WRONLY` (usually `_O_WRONLY | _O_CREAT | _O_TRUNC`) |
151151
| **`w+`** | `_O_RDWR` (usually **`_O_RDWR | _O_CREAT | _O_TRUNC`) |
152152
| **`b`** | `_O_BINARY` |
153-
| **`t`** | `_O_TEXT` |
153+
| **`t`** | `_O_TEXT` (translated) |
154154
| **`c`** | None |
155155
| **`n`** | None |
156-
| **`S`** | `_O_SEQUENTIAL` |
157-
| **`R`** | `_O_RANDOM` |
158-
| **`t`** | `_O_SHORTLIVED` |
159156
| **`D`** | `_O_TEMPORARY` |
157+
| **`R`** | `_O_RANDOM` |
158+
| **`S`** | `_O_SEQUENTIAL` |
159+
| **`T`** | `_O_SHORTLIVED` |
160160
| **`ccs=UNICODE`** | `_O_WTEXT` |
161161
| **`ccs=UTF-8`** | `_O_UTF8` |
162162
| **`ccs=UTF-16LE`** | `_O_UTF16` |
163163

164-
The **`c`**, **`n`**, and **`t`** *`mode`* options are Microsoft extensions for **`fopen_s`** and [`_fdopen`](fdopen-wfdopen.md) and shouldn't be used where you want ANSI portability.
164+
The **`c`**, **`n`**, **`R`**, **`S`**, **`t`**, **`T`**, and **`D`** *`mode`* options are Microsoft extensions for `fopen_s` and `_wfopen_s` and shouldn't be used when you want ANSI portability.
165165

166166
If you're using **`rb`** mode, memory mapped Win32 files might also be an option if you don't need to port your code, you expect to read much of the file, or you don't care about network performance.
167167

168+
Regarding `T` and `D`:
169+
- `T` avoids writing the file to disk as long as memory pressure doesn't require it. For more information, see `FILE_ATTRIBUTE_TEMPORARY` in [File attribute constants](/windows/win32/fileio/file-attribute-constants), and also this blog post [It's only temporary](https://learn.microsoft.com/archive/blogs/larryosterman/its-only-temporary).
170+
- `D` specifies a regular file that is written to disk. The difference is that it's automatically deleted when it's closed.
171+
You can combine `TD` to get both semantics.
172+
168173
## Requirements
169174

170175
| Function | Required header | C++ header |

docs/c-runtime-library/reference/fopen-wfopen.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: "Learn more about: fopen, _wfopen"
33
title: "fopen, _wfopen"
4-
ms.date: 05/18/2022
4+
ms.date: 04/27/2023
55
api_name: ["_wfopen", "fopen", "_o__wfopen", "_o_fopen"]
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"]
77
api_type: ["DLLExport"]
@@ -42,9 +42,9 @@ For more information, see [`errno`, `_doserrno`, `_sys_errlist`, and `_sys_nerr`
4242

4343
## Remarks
4444

45-
The **`fopen`** function opens the file that is specified by *`filename`*. By default, a narrow *`filename`* string is interpreted using the ANSI codepage (`CP_ACP`). In Windows Desktop applications, it can be changed to the OEM codepage (`CP_OEMCP`) by using the [`SetFileApisToOEM`](/windows/win32/api/fileapi/nf-fileapi-setfileapistooem) function. You can use the [`AreFileApisANSI`](/windows/win32/api/fileapi/nf-fileapi-arefileapisansi) function to determine whether *`filename`* is interpreted using the ANSI or the system default OEM codepage. **`_wfopen`** is a wide-character version of **`fopen`**; the **`_wfopen`** arguments are wide-character strings. Otherwise, **`_wfopen`** and **`fopen`** behave identically. Just using **`_wfopen`** doesn't affect the coded character set that's used in the file stream.
45+
The **`fopen`** function opens the file specified by *`filename`*. By default, a narrow *`filename`* string is interpreted using the ANSI codepage (`CP_ACP`). In Windows Desktop applications, it can be changed to the OEM codepage (`CP_OEMCP`) by using the [`SetFileApisToOEM`](/windows/win32/api/fileapi/nf-fileapi-setfileapistooem) function. You can use the [`AreFileApisANSI`](/windows/win32/api/fileapi/nf-fileapi-arefileapisansi) function to determine whether *`filename`* is interpreted using the ANSI or the system default OEM codepage. **`_wfopen`** is a wide-character version of **`fopen`**; the **`_wfopen`** arguments are wide-character strings. Otherwise, **`_wfopen`** and **`fopen`** behave identically. Just using **`_wfopen`** doesn't affect the coded character set that's used in the file stream.
4646

47-
**`fopen`** accepts paths that are valid on the file system at the point of execution; **`fopen`** accepts UNC paths and paths that involve mapped network drives as long as the system that executes the code has access to the share or mapped drive at the time of execution. When you construct paths for **`fopen`**, make sure that drives, paths, or network shares will be available in the execution environment. You can use either forward slashes (`/`) or backslashes (`\`) as the directory separators in a path.
47+
**`fopen`** accepts paths that are valid on the file system at the point of execution; **`fopen`** accepts UNC paths and paths that involve mapped network drives as long as the system that executes the code has access to the share or mapped drive at the time of execution. When you construct paths for **`fopen`**, make sure that drives, paths, or network shares are available in the execution environment. You can use either forward slashes (`/`) or backslashes (`\`) as the directory separators in a path.
4848

4949
Always check the return value to see whether the pointer is NULL before you perform any other operations on the file. If an error occurs, the global variable `errno` is set, and may be used to obtain specific error information. For more information, see [`errno`, `_doserrno`, `_sys_errlist`, and `_sys_nerr`](../errno-doserrno-sys-errlist-and-sys-nerr.md).
5050

@@ -60,7 +60,7 @@ Allowed values for **`ccs`** encoding are `UNICODE`, **`UTF-8`**, and **`UTF-16L
6060

6161
When a file is opened in Unicode mode, input functions translate the data that's read from the file into UTF-16 data stored as type **`wchar_t`**. Functions that write to a file opened in Unicode mode expect buffers that contain UTF-16 data stored as type **`wchar_t`**. If the file is encoded as UTF-8, then UTF-16 data is translated into UTF-8 when it's written. The file's UTF-8-encoded content is translated into UTF-16 when it's read. An attempt to read or write an odd number of bytes in Unicode mode causes a [parameter validation](../parameter-validation.md) error. To read or write data that's stored in your program as UTF-8, use a text or binary file mode instead of a Unicode mode. You're responsible for any required encoding translation.
6262

63-
If the file already exists and is opened for reading or appending, then any byte order mark (BOM) in the file determines the encoding. The BOM encoding takes precedence over the encoding that's specified by the **`ccs`** flag. The **`ccs`** encoding is only used when no BOM is present or the file is a new file.
63+
If the file already exists and is opened for reading or appending, then any byte order mark (BOM) in the file determines the encoding. The BOM encoding takes precedence over the encoding specified by the **`ccs`** flag. The **`ccs`** encoding is only used when no BOM is present or the file is a new file.
6464

6565
> [!NOTE]
6666
> BOM detection only applies to files that are opened in Unicode mode (that is, by passing the **`ccs`** flag).
@@ -77,7 +77,7 @@ The following table summarizes the modes that are used for various **`ccs`** fla
7777

7878
Files opened for writing in Unicode mode have a BOM written to them automatically.
7979

80-
If *`mode`* is **`a, ccs=encoding`** for some `encoding` value, **`fopen`** first tries to open the file by using both read and write access. If this action succeeds, the function reads the BOM to determine the encoding for the file. If it fails, the function uses the default encoding for the file. In either case, **`fopen`** will then reopen the file by using write-only access. (This behavior applies to **`"a"`** mode only, not to **`"a+"`** mode.)
80+
If *`mode`* is **`a, ccs=encoding`** for some `encoding` value, **`fopen`** first tries to open the file by using both read and write access. If this action succeeds, the function reads the BOM to determine the encoding for the file. If it fails, the function uses the default encoding for the file. In either case, **`fopen`** reopens the file using write-only access. (This behavior applies to **`"a"`** mode only, not to **`"a+"`** mode.)
8181

8282
### Generic-text routine mappings
8383

@@ -106,7 +106,7 @@ In addition to the earlier values, the following characters can be appended to *
106106

107107
| *`mode`* modifier | Translation mode |
108108
|--|--|
109-
| **`t`** | Open in text (translated) mode. |
109+
| **`t`** | Open in text (translated) mode. Carriage return-line feed (CR-LF) combinations are translated into single 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. |
110110
| **`b`** | Open in binary (untranslated) mode; translations involving carriage-return and line feed characters are suppressed. |
111111

112112
In text mode, `CTRL`+**Z** is interpreted as an EOF character on input. In files that are opened for reading/writing by using **`"a+"`**, **`fopen`** checks for a `CTRL`+**Z** at the end of the file and removes it, if it's possible. It's removed because using [`fseek`](fseek-fseeki64.md) and **`ftell`** to move within a file that ends with `CTRL`+**Z** may cause [`fseek`](fseek-fseeki64.md) to behave incorrectly near the end of the file.
@@ -127,8 +127,8 @@ The following options can be appended to *`mode`* to specify more behaviors.
127127
| **`N`** | Specifies that the file isn't inherited by child processes. |
128128
| **`S`** | Specifies that caching is optimized for, but not restricted to, sequential access from disk. |
129129
| **`R`** | Specifies that caching is optimized for, but not restricted to, random access from disk. |
130-
| **`T`** | Specifies a file as temporary. If possible, it isn't flushed to disk. |
131-
| **`D`** | Specifies a file as temporary. It's deleted when the last file pointer is closed. |
130+
| **`T`** | Specifies a file that isn't written to disk unless memory pressure requires it. |
131+
| **`D`** | Specifies a temporary file that's deleted when the last file pointer to it is closed. |
132132
| **`ccs=encoding`** | Specifies the encoded character set to use (one of **`UTF-8`**, **`UTF-16LE`**, or `UNICODE`) for this file. Leave unspecified if you want ANSI encoding. This flag is separated from flags that precede it by a comma (`,`). For example: `FILE *f = fopen("newfile.txt", "rt+, ccs=UTF-8");` |
133133

134134
Valid characters for the *`mode`* string that is used in **`fopen`** and **`_fdopen`** correspond to *`oflag`* arguments that are used in [`_open`](open-wopen.md) and [`_sopen`](sopen-wsopen.md), as follows.
@@ -142,7 +142,7 @@ Valid characters for the *`mode`* string that is used in **`fopen`** and **`_fdo
142142
| **`w`** | `_O_WRONLY` (usually `_O_WRONLY | _O_CREAT | _O_TRUNC`) |
143143
| **`w+`** | `_O_RDWR` (usually `_O_RDWR | _O_CREAT | _O_TRUNC`) |
144144
| **`b`** | `_O_BINARY` |
145-
| **`t`** | `_O_TEXT` |
145+
| **`t`** | `_O_TEXT` (translated) |
146146
| **`x`** | `_O_EXCL` |
147147
| **`c`** | None |
148148
| **`n`** | None |
@@ -156,6 +156,13 @@ Valid characters for the *`mode`* string that is used in **`fopen`** and **`_fdo
156156

157157
If you're using **`rb`** mode, you don't have to port your code, and if you expect to read most of a large file or aren't concerned about network performance, you might also consider whether to use memory mapped Win32 files as an option.
158158

159+
Regarding `T` and `D`:
160+
- `T` avoids writing the file to disk as long as memory pressure doesn't require it. For more information, see `FILE_ATTRIBUTE_TEMPORARY` in [File attribute constants](/windows/win32/fileio/file-attribute-constants), and also this blog post [It's only temporary](https://learn.microsoft.com/archive/blogs/larryosterman/its-only-temporary).
161+
- `D` specifies a regular file that is written to disk. The difference is that it's automatically deleted when it's closed.
162+
You can combine `TD` to get both semantics.
163+
164+
The **`c`**, **`n`**, **`R`**, **`S`**, **`t`**, **`T`**, and **`D`** *`mode`* options are Microsoft extensions for `fopen` and `_wfopen` and shouldn't be used when you want ANSI portability.
165+
159166
## Requirements
160167

161168
| Function | Required header |

0 commit comments

Comments
 (0)