sscanf用法簡析

2022-06-04 02:24:09 字數 1106 閱讀 3653

1. 常見用法。

char buf[512] = ;

sscanf("123456 ", "%s", buf);

printf("%s\n", buf);

結果為:123456

2. 取指定長度的字串。如在下例中,取最大長度為4位元組的字串。

sscanf("123456 ", "%4s", buf);

printf("%s\n", buf);

結果為:1234

3. 取到指定字元為止的字串。如在下例中,取遇到空格為止字串。

sscanf("123456 abcdedf", "%[^ ]", buf);

printf("%s\n", buf);

結果為:123456

4. 取僅包含指定字符集的字串。如在下例中,取僅包含1到9和小寫字母的字串。

sscanf("123456abcdedfbcdef", "%[1-9a-z]", buf);

printf("%s\n", buf);

結果為:123456abcdedf

5. 取到指定字符集為止的字串。如在下例中,取遇到大寫字母為止的字串。

sscanf("123456abcdedfbcdef", "%[^a-z]", buf);

printf("%s\n", buf);

結果為:123456abcdedf

6、給定乙個字串iios/12ddwdff@122,獲取 / 和 @ 之間的字串,先將 "iios/"過濾掉,再將非'@'的一串內容送到buf中

sscanf("iios/12ddwdff@122", "%*[^/]/%[^@]", buf);

printf("%s\n", buf);

結果為:12ddwdff

7、給定乙個字串「「hello, world」,僅保留world。(注意:「,」之後有一空格)

sscanf(「hello, world」, "%*s%s", buf);

printf("%s\n", buf);

結果為:world

還有,將乙個數字字串裝換成整數

#include

using namespace std;

int main()

DataReader的用法程式簡析

2015 07 05 using system using system.collections.generic using system.linq using system.text using system.threading.tasks using system.data using syst...

sscanf 函式用法

read formatted data from a string.intsscanf constchar buffer,constchar format argument intswscanf constwchar t buffer,constwchar t format argument a f...

SSCANF用法詳解

名稱 sscanf 從乙個字串中讀進與指定格式相符的資料.int sscanf const char const char int scanf const char include sscanf與scanf類似,都是用於輸入的,只是後者以鍵盤 stdin 為輸入源,前者以固定字串為輸入源。第乙個引數...