_fprintf_p
, _fprintf_p_l
, _fwprintf_p
, _fwprintf_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.3500000000000000008)會繼續四捨五入至 2.4。 這些函式完成的四捨五入現在也會遵守 所 fesetround
設定的浮點舍入模式。 之前,舍入一律選擇 FE_TONEAREST
行為。 這項變更只會影響使用 Visual Studio 2019 16.2 版和更新版本所建置的程式。 若要使用舊版浮點舍入行為,請連結至 'legacy_stdio_float_rounding.obj' 。
如同非安全版本(請參閱 fprintf
、、 _fprintf_l
、 _fwprintf_l
fwprintf
等),這些函式會驗證其參數並叫用不正確參數處理常式,如參數驗證 中所述 ,如果 stream
format
或 為 Null 指標,或是否有任何未知或格式不正確的格式規範。 若允許繼續執行,函式會傳回 -1,並將 errno
設為 EINVAL
。
泛型文字常式對應
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_l
, fwscanf
, _fwscanf_l
sprintf
, _sprintf_l
, swprintf
, _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_l
, fwscanf_s
, _fwscanf_s_l
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應