fprintf()関数の概要

4713 ワード

総じて言えば
printfは、スクリーンのような標準出力装置に出力するための標準出力ストリームの出力関数であり、
fprintfはファイルに出力し、出力された内容をハードディスク上のファイルやファイルに相当するデバイスに出力します.
printfはバッファ付き出力で、fprintfはバッファなし
 
fprintfの具体的な定義は以下の通りである.
 
Definition and Usageの定義と使用方法
The fprintf() function writes a formatted string to a specified output stream (example: file or database).fprintf()関数の役割は、フォーマットされた文字列をストリームに出力する/フォーマットされた文字列を出力ストリームに書き込むことです.
The arg1, arg2,++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first%sign,arg 1 is inserted,at the second%sign,arg 2 is inserted,etc.arg 1,arg 2,++パラメータが本体文字列のパーセンテージ(%)に挿入された後、この関数は「ステップ[step-by-step]」で実行されます.最初の「%」の後にarg 1を挿入し、2番目の「%」の後にarg 2を挿入し、順次類推します.
The fprintf() function returns the length of the written string.fprintf()関数は、書かれた文字列の長さを返します.
Syntax構文
fprintf(stream,format,arg1,arg2,arg++)

パラメータパラメータ
Descriptionの説明
stream
Required. Specifies where to write/output the stringに必要なパラメータ.文字列を書き込み/出力する場所を指定します.
format
Required. Specifies the string and how to format the variables in it. 必要なパラメータ.文字列を指定し、変数のフォーマットを定義します.Possible format values:次のような値があります.
  • %-Returns a percent sign%-戻りパーセント
  • %b-Binary number%b-戻りバイナリ数
  • %c-The character according to the ASCII value%c-ASCII値に対応する文字
  • を返します.
  • %d-Signed decimal number%d-正負の番号を持つ10進数
  • %e-Scientific notation(e.g.1.2 e+2)%e–科学カウント記号(1.2 e+2など)
  • %u-Unsigned decimal number%u-正負番号のない10進数
  • %f-Floating-point number(local settings aware)%f-浮動小数点データ(ローカル設定)
  • %F-Floating-point number(not local settings aware)%F-浮動小数点データ(ローカル設定以外)
  • %o-Octal number%o-10進数
  • %s-String%s-文字列
  • %x-Hexadecimal number(lowercase letters)%x-16進数(小文字)
  • %X-Hexadecimal number(uppercase letters)%X-16進数(大文字)
  • Additional format values. These are placed between the%and the letter(example%.2 f):その他のフォーマットの値.これは%とアルファベットの間にあります(%.2fなど)
  • +(Forces both+and-in front of numbers.By default,only negative numbers are marked)+(数値の前に+と-;デフォルトでは負の数だけがマークされています)
  • '(Specifies what to use as padding.Default is space.Must be used together with the width specifier.Example:%'x 20 s(this uses“x”as padding)'(補白として何を使用するかを指定し、デフォルト値はスペースです.幅指定器とともに使用する必要があります.たとえば:%'x 20 s(「x」をpaddingとして使用)
  • -(Left-justifies the variable value)-
  • [0-9](Specifies the minimum width held of to the variable value)[0-9](変数値の最小幅を指定)
  • .[0-9] (Specifies the number of decimal digits or maximum string length) .[0-9](10進数または最大文字列の長さを指定)
  • Note: If multiple additional format values are used, they must be in the same order as above.注:追加のフォーマット値を使用する場合は、上記の順序と同じでなければなりません.
    arg1
    Required. The argument to be inserted at the first%-sign in the format stringに必要なパラメータ.この引数(arg 1)は、最初の%-記号の前に挿入する必要があります.
    arg2
    Optional. The argument to be inserted at the second%-sign in the format stringオプションパラメータ.この引数(arg 2)は、2番目の%-記号の前に挿入する必要があります.
    arg++
    Optional. The argument to be inserted at the third,fourth,etc.%-sign in the format stringオプションパラメータ.上記の引数と同様に、3番目、4番目......(順次類推)%-記号の前に挿入できます.
    Tips and Notesヒントと注意点
    Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "$". See example three.注意:ここで%が引数よりも多い場合は、プレースホルダ[placeholders]を使用する必要があります.プレースホルダは%の後に挿入され、引数-数値と「$」から構成されます.具体的にはケース3を参照してください.
    Tip: Related functions: printf(), sprintf(), vfprintf(), vprintf(), and vsprintf().ヒント:関連関数:printf()、sprintf()、vfprintf()、vprintf()、およびvsprintf()
    Example 1ケース1
     

    The output of the code above will be:上記のコードは次の結果を出力します.
    27

    The following text will be written to the file「test.txt」:次のテキストがファイル「text.txt」に書き込まれます.
    Hello world. Day number 123

    Example 2ケース2

    The following text will be written to the file「test.txt」:次のテキストがファイル「text.txt」に書き込まれます.
    123.000000

    Example 3ケース3
    Use of placeholders:プレースホルダの使用

    The following text will be written to the file「test.txt」:次のテキストがファイル「text.txt」に書き込まれます.
    With 2 decimals: 123.00 
    With no decimals: 123