fprintf() 函數簡介

總的來說

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, arg1 is inserted, at the second % sign, arg2 is inserted, etc.
arg1, arg2, ++參數將被插入到主體字符串中的百分號(%)之後。這個函數是“一步一步[step-by-step]”執行的。在第一個“%”之後插入arg1,在第二個“%”之後插入arg2,依次類推。

The fprintf() function returns the length of the written string.
fprintf()函數將返回書寫的字符串長度。

Syntax
語法
fprintf(stream,format,arg1,arg2,arg++)

Parameter參數 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 –帶有正負號的十進制數
%e - Scientific notation (e.g. 1.2e+2)
%e –科學計數符號(如:1.2e+2)
%u - Unsigned decimal number
%u –不帶正負號的十進制數
%f - Floating-point number (local settings aware)
%f – 浮點數據(本地設置)
%F - Floating-point number (not local settings aware)
%F –浮點數據(非本地設置)
%o - Octal number
%o –十進制數
%s - String
%s –字符串
%x - Hexadecimal number (lowercase letters)
%x –十六進制數(小寫字母)
%X - Hexadecimal number (uppercase letters)
%X –十六進制數(大寫字母)
Additional format values. These are placed between the % and the letter (example %.2f):
其它格式的值。它是位於%和字母之間的(如:%.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: %'x20s (this uses "x" as padding)
’(指定使用什麼作爲補白,默認值是空格。它必須與寬度指定器一起使用。如:%'x20s(使用“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](指定十進制數值或最大字符串長度)
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
必要參數。這個自變量(arg1)必須安插在第一個%-符號前
arg2 Optional. The argument to be inserted at the second %-sign in the format string
可選參數。這個自變量(arg2)必須安插在第二個%-符號前
arg++ Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string
可選參數。與上述自變量相同,它們可以安插在第三個、第四個……(依次類推)%-符號前。

 

--------------------------------------------------------------------------------

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
<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo fprintf($file,"%s world. Day number %u",$str,$number);
?> 


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
<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"%f",$number);
?>


The following text will be written to the file "test.txt":
下面的文本將被寫入文件“text.txt”:

123.000000

 

--------------------------------------------------------------------------------

Example 3
案例3
Use of placeholders:
使用佔位符

<?php
$number = 123;
$file = fopen("test.txt","w");
fprintf($file,"With 2 decimals: %1$.2f
nWith no decimals: %1$u",$number);
?>


The following text will be written to the file "test.txt":
下面的文本將被寫入文件“text.txt”:

With 2 decimals: 123.00
With no decimals: 123

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/tiany524/archive/2009/12/17/5025506.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章