将格式化数据输出到流。
有关 _ftprintf_p 和 _ftprintf_p_l,请参阅 泛型文本函数映射。
语法
int _fprintf_p(
FILE *stream,
const char *format [,
argument ]...
);
int _fprintf_p_l(
FILE *stream,
const char *format,
_locale_t locale [,
argument ]...
);
int _fwprintf_p(
FILE *stream,
const wchar_t *format [,
argument ]...
);
int _fwprintf_p_l(
FILE *stream,
const wchar_t *format,
_locale_t locale [,
argument ]...
);
参数
stream
指向 FILE 结构的指针。
format
窗体控件字符串。
argument
可选参数。
locale
要使用的区域设置。
返回值
如果发生输出错误,则 _fprintf_p 和 _fwprintf_p 返回写入的字符数或一个负值。
备注
_fprintf_p 格式化一系列字符和值并将其输出到输出 stream。 每个 argument 函数(如果有)根据 format 中相应的格式规范进行转换和输出。 对于 _fprintf_p,format 参数具有与 _printf_p 中相同的语法。 这些函数支持位置参数,即可以更改格式字符串所使用的参数顺序。 有关位置参数的详细信息,请参阅 printf_p 位置参数。
_fwprintf_p 是 _fprintf_p 的宽字符版本;在 _fwprintf_p 中,format 是宽字符字符串。 如果在 ANSI 模式下打开流,则这些函数行为相同。
_fprintf_p 当前不支持到 UNICODE 流中的输出。
这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前区域设置。
重要
确保 format 不是用户定义的字符串。
从 Windows 10 版本 2004(内部版本 19041)开始,printf 系列函数根据 IEEE 754 的舍入规则输出可精确表示的浮点数。 在早期的 Windows 版本中,以“5”结尾并且可精确表示的浮点数总是向上取整。 IEEE 754 规定它们必须舍入到最接近的偶数(也称为“四舍六入五成双”)。 例如,printf("%1.0f", 1.5) 和 printf("%1.0f", 2.5) 都应舍入为 2。 之前,1.5 舍入为 2,2.5 舍入为 3。 此更改仅影响可精确表示的数字。 例如,2.35(用于内存表示时更接近于 2.35000000000000008)仍然向上取整为 2.4。 这些函数完成的舍入现在也遵循 fesetround 设置的浮点舍入模式。 以前,舍入始终选择 FE_TONEAREST 行为。 此更改仅影响使用 Visual Studio 2019 版本 16.2 及更高版本生成的程序。 若要使用旧浮点舍入行为,请链接到 legacy_stdio_float_rounding.obj。
与不安全版本一样(请参见 fprintf、_fprintf_l、fwprintf、_fwprintf_l),如果 或 stream 为空指针或者如果存在任何未知或格式不正确的格式设置说明符,则这些函数将验证其参数并调用无效参数处理程序,如format中所述。 如果允许执行继续,则这些函数将返回 -1 并将 errno 设置为 EINVAL。
泛型文本函数映射
列中的 tchar.h 函数映射到其他列中的函数,具体取决于编译时定义的字符集。
tchar.h 函数 |
_UNICODE 和 _MBCS 未定义 |
_MBCS 已定义 |
_UNICODE 已定义 |
|---|---|---|---|
_ftprintf_p |
_fprintf_p |
_fprintf_p |
_fwprintf_p |
_ftprintf_p_l |
_fprintf_p_l |
_fprintf_p_l |
_fwprintf_p_l |
有关详细信息,请参阅格式规范语法。
要求
| 函数 | 必需的标头 |
|---|---|
_fprintf_p、_fprintf_p_l |
<stdio.h> |
_fwprintf_p、_fwprintf_p_l |
<stdio.h> 或 <wchar.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// crt_fprintf_p.c
// This program uses _fprintf_p to format various
// data and print it to the file named FPRINTF_P.OUT. It
// then displays FPRINTF_P.OUT on the screen using the system
// function to invoke the operating-system TYPE command.
//
#include <stdio.h>
#include <process.h>
int main( void )
{
FILE *stream = NULL;
int i = 10;
double fp = 1.5;
char s[] = "this is a string";
char c = '\n';
// Open the file
if ( fopen_s( &stream, "fprintf_p.out", "w" ) == 0)
{
// Format and print data
_fprintf_p( stream, "%2$s%1$c", c, s );
_fprintf_p( stream, "%d\n", i );
_fprintf_p( stream, "%f\n", fp );
// Close the file
fclose( stream );
}
// Verify our data
system( "type fprintf_p.out" );
}
this is a string
10
1.500000
另请参阅
流 I/O
_cprintf、、_cprintf_l_cwprintf、、_cwprintf_l
fscanf、、_fscanf_lfwscanf、、_fwscanf_l
sprintf、、_sprintf_lswprintf、_swprintf_l、__swprintf_l
printf_p 位置参数
_cprintf_p、、_cprintf_p_l_cwprintf_p、、_cwprintf_p_l
_cprintf_s、、_cprintf_s_l_cwprintf_s、、_cwprintf_s_l
printf_p 位置参数
fscanf_s、、_fscanf_s_lfwscanf_s、、_fwscanf_s_l