定義和用法
The fscanf() function parses the input from an open file according to the specifIEd format.
fscanf()函數的作用是:從文件中按照指定的格式輸入。
Syntax
語法
fscanf(file,format,mixed)
Parameter參數Description描述
fileRequired. SpecifIEs the file to check
必要參數。指定文件對象
formatRequired. SpecifIEs the format.
必要參數。指定格式
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)
’(指定使用旁白[padding]的類型。默認是空格space,它必須同時指定寬度,例如:%'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.
注意:如果要使用多個上述的值,順序必須按照上面的順序,不能打亂。
mixedOptional.
可選參數
Tips and Notes
注意點
Note: Any whitespace in the format string matches any whitespace in the input stream. This means that a tab (/t) in the format string can match a single space character in the input stream.
注意:任何format的值中的空白部分[whitespace]與input內容相似;這意味著tab鍵(/t)[制表符]和input內容的一個單一的空格[space]相似。