1. 程式人生 > >C的scanf 和 C++的fscanf 的用法

C的scanf 和 C++的fscanf 的用法

com bracket ren otherwise wid and space out types

說明:本文不適合新手學習,適合用來做參考。本文參考有其他博客的內容,不過年代久遠已經忘記了,在此感謝各位博主! scanf函數 用 法:int scanf(char *format[,argument,...]); //scanf()函數是通用終端格式化輸入函數,它從標準輸入設備(鍵盤) 讀取輸入的信息。可以讀入任何固有類型的數據並自動把數值變換成適當的機內格式。 調用格式: scanf("<格式化字符串>",<地址表>); // scanf()函數返回成功賦值的數據項數,出錯時則返回EOF。 其控制串由三類字符構成:1、格式化說明符;2、空白符;
3、非空白符; 1)格式化說明符
格式字符 說明
%a、%A 讀入一個浮點值(僅C99有效)
%f 、 %F、%e、 %E、%g 、%G 讀入一個浮點數
%c、%C 讀入一個字符
%d 讀入十進制整數
%s 讀入一個字符串
%p 讀入一個指針
%u 讀入一個無符號十進制整數
%n 至此已讀入值的等價字符數
%[] 掃描字符集合
%% 讀%符號
%o 讀入八進制整數
%x 、%X 讀入十六進制整數
%i 讀入十進制,八進制,十六進制整數
2)空白字符 空白字符會使scanf()函數在讀操作中略去輸入中的一個或多個空白字符,空白符可以是space,tab,newline等等,直到第一個非空白符出現為止。 3)非空白字符 一個非空白字符會使scanf()函數在讀入時剔除掉與這個非空白字符相同的字符。 fscanf函數 A format specifier for fscanf follows this prototype: % [*][width][length]specifier The format specifier contain sub-specifiers:
asterisk (*), width and length (in that order), which are optional and follow these specifications:
sub-specifierdescription
* An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument). 註:加上*可將該位置數據忽略
width Specifies the maximum number of characters to be read in the current reading operation (optional).
註:比如填入7.3,表示整數部分寬度為7,小數部分只讀到第3位;也可以不填,不填則將對應數據不帶舍入地讀入
length One of hh, h, l, ll, j, z, t, L (optional).
This alters the expected type of the storage pointed by the corresponding argument (see the last table).
Where the specifier character at the end is the most significant component, since it defines which characters are extracted, their interpretation and the type of its corresponding argument:
specifierDescriptionCharacters extracted
i, u Integer Any number of digits, optionally preceded by a sign (+ or -).
Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f).
d Decimal integer
(十進制數)
Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
o Octal integer Any number of octal digits (0-7), optionally preceded by a sign (+ or -).
x Hexadecimal integer (16進制數) Any number of hexadecimal digits (0-9, a-f, A-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -).
f, e, g Floating point
number (浮點數)
A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod).
Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
a
c Character The next character. If a width other than 1 is specified, the function reads exactlywidth characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.
s String of characters Any number of non-whitespace characters, stopping at the first whitespacecharacter found. A terminating null character is automatically added at the end of the stored sequence.
p Pointer address A sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %p in fprintf.
[characters] Scanset Any number of the characters specified between the brackets.
A dash (-) that is not the first character may produce non-portable behavior in some library implementations.
[^characters] Negated scanset Any number of characters none of them specified as characters between the brackets.
n Count No input is consumed.
The number of characters read so far from stream is stored in the pointed location.
% % A % followed by another % matches a single %.
Except for n, at least one character shall be consumed by any specifier. Otherwise the match fails, and the scan ends there.
This is a chart showing the types expected for the corresponding arguments where input is stored (both with and without a length sub-specifier):(length放在specifiers前會產生不同的效果,更具體。也可以選擇不使用)
specifiers
lengthd iu o xf e g ac s [] [^]pn
(none) int* unsigned int* float* char* void** int*
hh signed char* unsigned char* signed char*
h short int* unsigned short int* short int*
l long int* unsigned long int* double* wchar_t* long int*
ll long long int* unsigned long long int* long long int*
j intmax_t* uintmax_t* intmax_t*
z size_t* size_t* size_t*
t ptrdiff_t* ptrdiff_t* ptrdiff_t*
L long double*
Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99.

C的scanf 和 C++的fscanf 的用法