Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. fopen, _wfopen
In this articleOpens a file. More-secure versions of these
functions that perform more parameter validation and return error codes are available; see Syntax
Parameters
Return valueEach of these
functions returns a pointer to the open file. A null pointer value indicates an error. If For more information, see RemarksThe
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 By default, this function's global state is scoped to the application. To change it, see Global state in the CRT. Unicode support
Allowed values for
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 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 Note BOM detection only applies to files that are opened in Unicode mode (that is, by passing the
The following table summarizes the modes that are used for various Encodings used based on ccs flag and BOM
Files opened for writing in Unicode mode have a BOM written to them automatically. If Generic-text routine mappings
The character string
When a file is opened by using the The When the In addition to the earlier values, the following characters can be appended to
In text mode, 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. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the If For more information about how to use text and binary modes in Unicode and multibyte stream-I/O, see Text and binary mode file I/O and Unicode stream I/O in text and binary modes. The following options can be appended to
Valid characters for the
If you're using Requirements
The Example 1The following program opens two files. It uses
Example 2The following program creates a file (or overwrites one if it exists), in
text mode that has Unicode encoding. It then writes two strings into the file and closes the file. The output is a file named
See alsoStream I/O FeedbackSubmit and view feedback for Which type of data file access allows access to any piece of data in the file without reading the data that comes before it?1. When working with a sequential access file, you can jump directly to any piece of data in the file without reading the data that comes before it. 2. When an input file is opened, its read position is initially set to the first item in the file.
Which method could be used to strip specific characters from the end of a string?TrimEnd. The String. TrimEnd method removes characters from the end of a string, creating a new string object. An array of characters is passed to this method to specify the characters to be removed.
Which method will return an empty string when it has attempted to read beyond the end of a file?Note that the readline method returns an empty string ( "" ) when it attempt to read beyond the end of the file.
What method or operator can be used to concatenate lists?The '+' operator can be used to concatenate two lists. It appends one list at the end of the other list and results in a new list as output.
|