In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this:
fopen("newfile.txt", "rw, ccs=<encoding>");
Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file.
Flag
No BOM (or new file)
BOM: UTF-8
BOM: UTF-16
UNICODE
ANSI
UTF-8
UTF-16LE
UTF-8
UTF-8
UTF-8
UTF-16LE
UTF-16LE
UTF-16LE
UTF-8
UTF-16LE
If mode is "a, ccs=<encoding>", fopen will first try to open the file with both read and write access. If it succeeds, it will read the BOM to determine the encoding for this file; however, if it fails, it will use the default encoding for the file. In either case, fopen will then re-open the file with write-only access. (This applies to mode a only, not a+.)
fprintf, fwprintf
Print formatted data to a stream.
int fprintf( FILE *stream, const char *format [, argument ]...);
int fwprintf( FILE *stream, const wchar_t *format [, argument ]...);
Function Required Header Compatibility
fprintf <stdio.h> ANSI, Win 95, Win NT
fwprintf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
fprintf returns the number of bytes written. fwprintf returns the number of wide characters written. Each of these functions returns a negative value instead when an output error occurs.
Parameters
stream
Pointer to FILE structure
format
Format-control string
argument
Optional arguments
Remarks
fprintf formats and prints a series of characters and values to the output stream. Each function argument (if any) is converted and output according to the corresponding format specification in format. For fprintf, the format argument has the same syntax and use that it has in printf.
fwprintf is a wide-character version of fprintf; in fwprintf, format is a wide-character string. These functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_ftprintf fprintf fprintf fwprintf
For more information, see Format Specifcations.
Example
/* FPRINTF.C: This program uses fprintf to format various
* data and print it to the file named FPRINTF.OUT. It
* then displays FPRINTF.OUT on the screen using the system
* function to invoke the operating-system TYPE command.
*/
#include <stdio.h>
#include <process.h>
FILE *stream;
void main( void )
{
int i = 10;
double fp = 1.5;
char s[] = "this is a string";
char c = '\n';
stream = fopen( "fprintf.out", "w" );
fprintf( stream, "%s%c", s, c );
fprintf( stream, "%d\n", i );
fprintf( stream, "%f\n", fp );
fclose( stream );
system( "type fprintf.out" );
}
Output
this is a string
10
1.500000
Stream I/O Routines
See Also _cprintf, fscanf, sprintf