fprintf、_fprintf_l、fwprintf、_fwprintf_lfprintf, _fprintf_l, fwprintf, _fwprintf_l

将格式化数据输出到流。Print formatted data to a stream. 提供这些函数的更多安全版本;请参阅 fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_lMore secure versions of these functions are available; see fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l.

语法Syntax

int fprintf(
   FILE *stream,
   const char *format [,
   argument ]...
);
int _fprintf_l(
   FILE *stream,
   const char *format,
   locale_t locale [,
   argument ]...
);
int fwprintf(
   FILE *stream,
   const wchar_t *format [,
   argument ]...
);
int _fwprintf_l(
   FILE *stream,
   const wchar_t *format,
   locale_t locale [,
   argument ]...
);

参数Parameters

stream
指向文件结构的指针。Pointer to FILE structure.

formatformat
窗体控件字符串。Format-control string.

自变量argument
可选参数。Optional arguments.

localelocale
要使用的区域设置。The locale to use.

返回值Return Value

fprintf返回写入的字节数。fprintf returns the number of bytes written. fwprintf返回写入的宽字符数。fwprintf returns the number of wide characters written. 其中每个函数在出现输出错误时返回一个负值。Each of these functions returns a negative value instead when an output error occurs. 如果格式NULL中, 所述,这些函数将调用无效参数处理程序,参数验证If stream or format is NULL, these functions invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,函数将返回-1 并设置errnoEINVALIf execution is allowed to continue, the functions return -1 and set errno to EINVAL. 按原样使用时未的有效格式设置字符检查格式字符串fprintf_sfwprintf_sThe format string is not checked for valid formatting characters as it is when using fprintf_s or fwprintf_s.

有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.

备注Remarks

fprintf格式和输出一系列字符和值输出到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. 有关fprintf格式自变量具有相同的语法和使用该中printfFor fprintf, the format argument has the same syntax and use that it has in printf.

fwprintf是宽字符版本的fprintf; 在fwprintf格式是宽字符字符串。fwprintf is a wide-character version of fprintf; in fwprintf, format is a wide-character string. 如果在 ANSI 模式下打开流,则这些函数行为相同。These functions behave identically if the stream is opened in ANSI mode. fprintf当前不支持输出到 UNICODE 流。fprintf does not currently support output into a UNICODE stream.

使用这些函数的版本 _l后缀是相同,只不过它们使用传递而不是当前线程区域设置的区域设置参数。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

重要

确保 format 不是用户定义的字符串。Ensure that format is not a user-defined string.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_ftprintf_ftprintf fprintffprintf fprintffprintf fwprintffwprintf
_ftprintf_l_ftprintf_l _fprintf_l_fprintf_l _fprintf_l_fprintf_l _fwprintf_l_fwprintf_l

有关更多信息,请参见 格式规范For more information, see Format Specifications.

要求Requirements

函数Function 必需的标头Required header
fprintf_fprintf_lfprintf, _fprintf_l <stdio.h><stdio.h>
fwprintf_fwprintf_lfwprintf, _fwprintf_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_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;

int main( void )
{
   int    i = 10;
   double fp = 1.5;
   char   s[] = "this is a string";
   char   c = '\n';

   fopen_s( &stream, "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" );
}
this is a string
10
1.500000

请参阅See also

流 I/OStream I/O
_cprintf、_cprintf_l、_cwprintf、_cwprintf_l_cprintf, _cprintf_l, _cwprintf, _cwprintf_l
fscanf、_fscanf_l、fwscanf、_fwscanf_lfscanf, _fscanf_l, fwscanf, _fwscanf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
格式规范语法:printf 和 wprintf 函数Format Specification Syntax: printf and wprintf Functions