?
This document uses PHP Chinese website manual Release
在頭文件<wchar.h>中定義 | ||
---|---|---|
(1) | ||
int wscanf(const wchar_t * format,...); | (自C95以來)(直到C99) | |
int wscanf(const wchar_t * restrict format,...); | (自C99以來) | |
(2) | ||
int fwscanf(FILE * stream,const wchar_t * format,...); | (自C95以來)(直到C99) | |
int fwscanf(FILE * restrict stream,const wchar_t * restrict format,...); | (自C99以來) | |
(3) | ||
int swscanf(const wchar_t * buffer,const wchar_t * format,...); | (自C95以來)(直到C99) | |
int swscanf(const wchar_t * restrict buffer,const wchar_t * restrict format,...); | (自C99以來) | |
int wscanf_s(const wchar_t * restrict format,...); | (4) | (自C11以來) |
int fwscanf_s(FILE * restrict stream,const wchar_t * restrict format,...); | (5) | (自C11以來) |
int swscanf_s(const wchar_t * restrict s,const wchar_t * restrict format,...); | (6) | (自C11以來) |
非數(shù)字轉(zhuǎn)換為nan
或。使用哪一個是實現(xiàn)定義的。nan(char_sequence)
該轉(zhuǎn)換F
,E
,G
,A
輸出INF
,INFINITY
,NAN
來代替。
即使%c
需要int
參數(shù),通過char
調(diào)用可變參數(shù)函數(shù)時發(fā)生的整數(shù)提升也是安全的。
對于固定寬度的字符類型(正確的轉(zhuǎn)換規(guī)格int8_t
<inttypes.h>還(雖然,等等)都在頭定義PRIdMAX
,PRIuMAX
等是同義詞%jd
,%ju
等)。
內(nèi)存寫入轉(zhuǎn)換說明符%n
是安全漏洞的常見目標(biāo),其中格式字符串取決于用戶輸入,并且不受邊界檢查printf_s
函數(shù)族的支持。
每個轉(zhuǎn)換說明符的操作之后都有一個序列點(diǎn); 這允許將多個%n
結(jié)果存儲在相同的變量中,或者作為邊緣情況,%n
在同一個調(diào)用中打印由較早修改的字符串。
如果轉(zhuǎn)換規(guī)范無效,則行為未定義。
... - arguments specifying data to print
1,2)如果發(fā)生錯誤,則成功寫入寬字符數(shù)或?qū)懭胴?fù)值。
3)如果編碼錯誤發(fā)生或者如果要生成的字符數(shù)等于或大于size
(包括當(dāng)size
為零時),則寫入的寬字符的數(shù)目(不包括終止空寬字符)如果成功或負(fù)值。
4,5)如果發(fā)生錯誤,則成功寫入寬字符數(shù)或?qū)懭胴?fù)值。
6)寫入的寬字符數(shù)(不包括終止空值)buffer
。返回編碼錯誤和溢出時的負(fù)值。對所有其他錯誤返回零。
7)將已經(jīng)寫入寬字符(不包括終止空)數(shù)量buffer
已經(jīng)bufsz
如果發(fā)生錯誤,已經(jīng)足夠大時,或為負(fù)值。(也就是說,只有當(dāng)回報是非負(fù)的且小于時,寫才能成功并且完成bufsz
)
雖然窄字符串提供snprintf
了可以確定所需的輸出緩沖區(qū)大小的可能性,但對于寬字符串沒有等效(直到C11的snwprintf_s),并且為了確定緩沖區(qū)大小,程序可能需要調(diào)用swprintf
,檢查結(jié)果值,并重新分配一個更大的緩沖區(qū),再次嘗試直到成功。
snwprintf_s
不同swprintf_s
,將截斷結(jié)果以適應(yīng)指向的數(shù)組buffer
,即使截斷被大多數(shù)邊界檢查函數(shù)視為錯誤。
N/A N/A char*
#include <locale.h>#include <wchar.h> int main(void){ char narrow_str[] = "z\u00df\u6c34\U0001f34c"; // or "z?水?" // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c"; wchar_t warr[29]; // the expected string is 28 characters plus 1 null terminator setlocale(LC_ALL, "en_US.utf8"); swprintf(warr, sizeof warr/sizeof *warr, L"Converted from UTF-8: '%s'", narrow_str); wprintf(L"%ls\n", warr);}
N / AN / AN / AN / AN / A s
匹配非空白字符序列(字符串)如果使用寬度說明符,則匹配寬度或直到第一個空白字符,以先出現(xiàn)者為準(zhǔn)。除了匹配的字符外,總是存儲一個空字符(所以參數(shù)數(shù)組必須至少有寬度+ 1個字符的空間)。
輸出:
`d` matches a **decimal integer**. The format of the number is the same as expected by [`wcstol()`](../string/wide/wcstol) with the value `10` for the `base` argument.
Converted from UTF-8: 'z?水?'
`signed short*` or `unsigned short*`
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.29.2.1 fwprintf函數(shù)(p:403-410)
7.29.2.3 swprintf函數(shù)(p:416)
7.29.2.11 wprintf函數(shù)(p:421)
K.3.9.1.1 fwprintf_s函數(shù)(p:628)
K.3.9.1.4 swprintf_s函數(shù)(p:630-631)
K.3.9.1.13 wprintf_s函數(shù)(p:637-638)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.24.2.1 fwprintf函數(shù)(p:349-356)
7.24.2.3 swprintf函數(shù)(p:362)
7.24.2.11 wprintf函數(shù)(p:366)
`signed long long*` or `unsigned long long*`
[`intmax_t`](../types/integer)`*` or [`uintmax_t`](../types/integer)`*`
size_t*
ptrdiff_t*
N / A i
匹配整數(shù)。數(shù)的格式是相同的通過按預(yù)期wcstol()
與值0
的base
參數(shù)(基部由解析的第一字符確定)。
`u` matches an unsigned **decimal integer**. The format of the number is the same as expected by [`wcstoul()`](../string/wide/wcstoul) with the value `10` for the `base` argument.
`o` matches an unsigned **octal integer**. The format of the number is the same as expected by [`wcstoul()`](../string/wide/wcstoul) with the value `8` for the `base` argument.
`x`, `X` matches an unsigned **hexadecimal integer**. The format of the number is the same as expected by [`wcstoul()`](../string/wide/wcstoul) with the value `16` for the `base` argument.
`n` returns the **number of characters read so far**. No input is consumed. Does not increment the assignment count. If the specifier has assignment-suppressing operator defined, the behavior is undefined.
`a`, `A`(C99)
e
, E
f
, F
g
,G
匹配一個浮點(diǎn)數(shù)。數(shù)字的格式與預(yù)期的相同wcstof()
。
N/A N/A float*
double*
N/A N/A N/A N/A long double*
`p` matches implementation defined character sequence defining a **pointer**. `printf` family of functions should produce the same sequence using `%p` format specifier.
N/A N/A void**
n
不適用不適用不適用不適用不適用不適用不適用不適用于任何指定字段寬度的最長輸入字符序列的 每個轉(zhuǎn)換說明符,或者正是轉(zhuǎn)換說明符所期望的或者是它期望的順序是從流中消耗的。在消耗序列之后的第一個字符(如果有的話)仍然未讀。如果消耗的序列長度為零或消費(fèi)的序列不能如上所述進(jìn)行轉(zhuǎn)換,則會發(fā)生匹配失敗,除非文件結(jié)束,編碼錯誤或讀取錯誤阻止了來自流的輸入,在這種情況下,它是輸入失敗。
在嘗試解析輸入之前,除了[
,,之外的所有轉(zhuǎn)換說明符都會消耗并放棄所有前導(dǎo)空白字符(如同通過調(diào)用一樣確定)。這些消耗的字符不會計入指定的最大字段寬度。cniswspace
如果l
未使用長度說明符,則轉(zhuǎn)換說明符c
,s
并[
執(zhí)行寬到多字節(jié)字符轉(zhuǎn)換,就像通過在第一個字符轉(zhuǎn)換之前使用初始化為零wcrtomb()
的mbstate_t
對象調(diào)用一樣。
除了匹配的字符之外,轉(zhuǎn)換說明符s
并[
始終存儲空終止符。目標(biāo)數(shù)組的大小必須至少比指定的字段寬度大1。在不指定目標(biāo)數(shù)組大小的情況下,使用%s
或%[
不安全gets
。
對于固定寬度的整數(shù)類型(正確的轉(zhuǎn)換規(guī)格int8_t
<inttypes.h>還(雖然,等等)都在頭定義SCNdMAX
,SCNuMAX
等是同義詞%jd
,%ju
等)。
每個轉(zhuǎn)換說明符的操作之后都有一個序列點(diǎn); 這允許將多個字段存儲在相同的“匯”變量中。
在解析指數(shù)中不包含數(shù)字的不完整浮點(diǎn)值時(例如"100er"
使用轉(zhuǎn)換說明符進(jìn)行解析),會消耗%f
序列"100e"
(可能有效的浮點(diǎn)數(shù)的最長前綴),從而導(dǎo)致匹配錯誤(消耗的序列不能轉(zhuǎn)換為浮點(diǎn)數(shù)),"r"
剩余的。現(xiàn)有的實現(xiàn)不遵循此規(guī)則并僅回滾消耗"100"
,只留下"er"
例如glibc錯誤1765。
... - receiving arguments
1-3)成功分配的接收參數(shù)數(shù)目,或者EOF
在分配第一個接收參數(shù)前發(fā)生讀取失敗。
4-6)與(1-3)相同,但是EOF
如果存在運(yùn)行時約束沖突,也會返回。
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.29.2.2 fwscanf功能(p:410-416)
7.29.2.4 swscanf函數(shù)(p:417)
7.29.2.12 wscanf函數(shù)(p:421)
K.3.9.1.2 fwscanf_s函數(shù)(p:628-629)
K.3.9.1.5 swscanf_s函數(shù)(p:631)
K.3.9.1.14 wscanf_s函數(shù)(p:638)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.24.2.2 fwscanf函數(shù)(p:356-362)
7.24.2.4 swscanf函數(shù)(p:362)
7.24.2.12 wscanf函數(shù)(p:366-367)