fprintf, _fprintf_l, fwprintf, _fwprintf_l

書式付きデータをストリームに出力します。 これらの関数のセキュリティを強化したバージョンを使用できます。「fprintf_s_fprintf_s_lfwprintf_s_fwprintf_s_l」を参照してください。

構文

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 ]...
);

パラメーター

stream
FILE 構造体へのポインター。

format
書式指定文字列。

argument
省略可能な引数。

locale
使用するロケール。

戻り値

fprintf は、書き込まれたバイト数を返します。 fwprintf は、書き込まれたワイド文字数を返します。 これらの関数は、出力エラーが発生した場合、負の値を返します。 その場合streamformatこれらの関数はNULL、「パラメーターの検証」で説明されているように、無効なパラメーター ハンドラーを呼び出します。 実行の継続が許可された場合、関数は -1 を返し、errnoEINVAL に設定します。 書式指定文字列は、有効な書式指定文字に対してチェックされません。使用する場合fprintf_s、または fwprintf_s.

リターン コードの詳細については、「errno_doserrno_sys_errlist、および _sys_nerr」を参照してください。

解説

fprintf は、一連の文字および値を書式設定し、出力 stream に出力します。 各関数の argument (指定されている場合) は、format 中の対応する書式指定に応じて変換され、格納されます。 の場合 fprintfformat 引数の構文 printfは .

fwprintffprintf のワイド文字バージョンで、fwprintf 内の format はワイド文字列です。 ストリームが ANSI モードで開かれている場合、これらの関数の動作は同じになります。 fprintf では、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' にリンクします。

汎用テキスト ルーチンのマップ

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_ftprintf fprintf fprintf fwprintf
_ftprintf_l _fprintf_l _fprintf_l _fwprintf_l

詳細については、「書式指定構文」を参照してください

必要条件

機能 必須ヘッダー
fprintf, _fprintf_l <stdio.h>
fwprintf, _fwprintf_l <stdio.h> または <wchar.h>

互換性の詳細については、「 Compatibility」を参照してください。

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

関連項目

ストリーム入出力
_cprintf, _cprintf_l, _cwprintf, _cwprintf_l
fscanf, _fscanf_l, fwscanf, _fwscanf_l
sprintf, _sprintf_l, swprintf, _swprintf_l, _swprintf_l
書式指定の構文: printf および wprintf 関数