fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_lfprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l

将格式化数据输出到流。Print formatted data to a stream. CRT 中的安全功能中所述,这些版本的 fprintf、_fprintf_l、fwprintf、_fwprintf_l 具有安全增强功能。These are versions of fprintf, _fprintf_l, fwprintf, _fwprintf_l with security enhancements as described in Security Features in the CRT.

语法Syntax

int fprintf_s(   
   FILE *stream,  
   const char *format [,  
   argument_list ]  
);  
int _fprintf_s_l(   
   FILE *stream,  
   const char *format,  
   locale_t locale [,  
   argument_list ]  
);  
int fwprintf_s(   
   FILE *stream,  
   const wchar_t *format [,  
   argument_list ]  
);  
int _fwprintf_s_l(   
   FILE *stream,  
   const wchar_t *format,  
   locale_t locale [,  
   argument_list ]  
);  

参数Parameters

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

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

argument_list
格式字符串的可选自变量。Optional arguments to the format string.

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

返回值Return Value

fprintf_s 返回已写入的字节数。fprintf_s returns the number of bytes written. fwprintf_s 返回已写入的宽字符数。fwprintf_s returns the number of wide characters written. 其中每个函数在出现输出错误时返回一个负值。Each of these functions returns a negative value instead when an output error occurs.

备注Remarks

fprintf_s 格式化一系列字符和值并将其输出到输出 streamfprintf_s formats and prints a series of characters and values to the output stream. 在每个自变量argument_list(如果有) 进行转换和输出中的相应格式规范根据formatEach argument in argument_list (if any) is converted and output according to the corresponding format specification in format. format自变量使用格式规范语法 printf 和 wprintf 函数The format argument uses the format specification syntax for printf and wprintf functions.

fwprintf_sfprintf_s 的宽字符版本;在 fwprintf_s 中,format 是宽字符字符串。fwprintf_s is a wide-character version of fprintf_s; in fwprintf_s, format is a wide-character string. 如果在 ANSI 模式下打开流,则这些函数行为相同。These functions behave identically if the stream is opened in ANSI mode. fprintf_s 当前不支持到 UNICODE 流中的输出。fprintf_s doesn't 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 locale.

重要

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

与不安全版本一样(请参见 fprintf、_fprintf_l、fwprintf、_fwprintf_l),如果 streamformat 为 null 指针,则这些函数将验证其参数并调用无效参数处理程序,如参数验证中所述。Like the non-secure versions (see fprintf, _fprintf_l, fwprintf, _fwprintf_l), these functions validate their parameters and invoke the invalid parameter handler, as described in Parameter Validation, if either stream or format is a null pointer. 格式字符串本身也需进行验证。The format string itself is also validated. 如果有任何未知或格式错误的格式化说明符,则这些函数将生成无效参数异常。If there are any unknown or badly formed formatting specifiers, these functions generate the invalid parameter exception. 在所有情况下,如果允许执行继续,则这些函数将返回 -1 并将 errno 设置为 EINVALIn all cases, If execution is allowed to continue, the functions return -1 and set errno to EINVAL. 有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_ftprintf_s fprintf_s fprintf_s fwprintf_s
_ftprintf_s_l _fprintf_s_l _fprintf_s_l _fwprintf_s_l

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

惠?Requirements

函数Function 必需的标头Required header
fprintf_s, _fprintf_s_lfprintf_s, _fprintf_s_l <stdio.h><stdio.h>
fwprintf_s, _fwprintf_s_lfwprintf_s, _fwprintf_s_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参见“简介”中的 兼容性For additional compatibility information, see Compatibility in the Introduction.

示例Example

// crt_fprintf_s.c  
// This program uses fprintf_s to format various  
// data and print it to the file named FPRINTF_S.OUT. It  
// then displays FPRINTF_S.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_s.out", "w" );  
   fprintf_s( stream, "%s%c", s, c );  
   fprintf_s( stream, "%d\n", i );  
   fprintf_s( stream, "%f\n", fp );  
   fclose( stream );  
   system( "type fprintf_s.out" );  
}  
this is a string  
10  
1.500000  

请参阅See Also

流 I/O Stream I/O
_cprintf、_cprintf_l、_cwprintf、_cwprintf_l _cprintf, _cprintf_l, _cwprintf, _cwprintf_l
fscanf、_fscanf_l、fwscanf、_fwscanf_l fscanf, _fscanf_l, fwscanf, _fwscanf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l