次の方法で共有


fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_l

ストリームに出力書式付きデータ。 これらの関数は、「CRT のセキュリティ機能」に説明されているように、fprintf、_fprintf_l、fwprintf、_fwprintf_l のセキュリティが強化されたバージョンです。

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

パラメーター

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

  • format
    書式指定文字列。

  • argument
    省略可能な引数。

  • locale
    使用するロケール。

戻り値

fprintf_s を書き込むバイト数を返します。 fwprintf_s を書き込むワイド文字数を返します。 これらの関数は、出力エラーが発生した場合は負の値を返します。

解説

fprintf_s は 出力 stream、一連の文字と値の書式を指定して、出力します*。*各関数 argument は formatの対応する書式指定に従って (存在する場合) に変換され、出力fprintf_sでは、format 引数に printf_sと同じ構文を使用できます。

fwprintf_s は fprintf_sのワイド文字バージョンであり、; fwprintf_sで、format はワイド文字列です。 ストリームが ANSI モードで開かれている場合、これらの関数の動作は同じになります。 fprintf_s では、UNICODE ストリームへの出力はサポートされていません。

_l サフィックスが付いているこれらの関数の各バージョンは、現在のロケールの代わりに渡されたロケール パラメーターを使用する点を除いて同じです。

セキュリティに関するメモセキュリティに関するメモ

format にユーザー定義の文字列を指定しないでください。

セキュリティが万全ではないバージョンと同様に stream または format が null ポインターの場合、(fprintf、_fprintf_l、fwprintf、_fwprintf_lを参照してください) は、これらの関数は、パラメーターを検証し、パラメーターの検証"に説明されているように、無効なパラメーター ハンドラーを呼び出します。 これらの関数は、セキュリティが万全ではないバージョンと書式指定文字列自体の検証も行うことです。 未知の書式指定子や作成の指定子がある場合、これらの関数は無効なパラメーターの例外を生成します。 すべての場合において、実行の継続が許可された場合、関数は -1 を返し、errno を EINVAL に設定します。 エラー コードの詳細については、「_doserrno、errno、_sys_errlist、および _sys_nerr」を参照してください。

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

TCHAR.H のルーチン

_UNICODE & _MBCS が未定義の場合

_MBCS が定義されている場合

_UNICODE が定義されている場合

_ftprintf_s

fprintf_s

fprintf_s

fwprintf_s

_ftprintf_s_l

_fprintf_s_l

_fprintf_s_l

_fwprintf_s_l

詳細については、「scanf 関数と wscanf 関数の書式指定フィールド」を参照してください。

必要条件

関数

必須ヘッダー

fprintf_s, _fprintf_s_l

<stdio.h>

fwprintf_s, _fwprintf_s_l

<stdio.h> または <wchar.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

// 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" );
}
  

同等の .NET Framework 関数

System::IO::StreamWriter::Write

参照

関連項目

ストリーム入出力

_cprintf、_cprintf_l、_cwprintf、_cwprintf_l

fscanf、_fscanf_l、fwscanf、_fwscanf_l

sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l