共用方式為


_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.35000000000000008) 會繼續四捨五入至 2.4。 這些函式所完成的四捨五入現在也會遵守 fesetround 所設定的浮點數四捨五入模式。 之前,四捨五入一律會選擇 FE_TONEAREST 行為。 這項變更只會影響使用 Visual Studio 2019 16.2 版和更新版本所建置的程式。 若要使用舊版浮點數四捨五入行為,請連結至「legacy_stdio_float_rounding.obj」

如同非安全版本(請參閱 fprintf、、_fprintf_l_fwprintf_lfwprintf等),這些函式會驗證其參數並叫用無效的參數處理程式,如參數驗證中所述,如果 stream format 或 為 Null 指標,或是否有任何未知或格式不正確的格式規範。 若允許繼續執行,函式會傳回 -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