一個衆所周知的scanf()
功能的功能是可以傳遞的格式根據該格式進行掃描輸入。如何讀取字符串,直到兩個連續的空格?
對於我的情況,我似乎無法找到搜索this和this文檔的解決方案。
我有一個字符串(sInput
),如下所示:
#something VAR1 this is a constant string //some comment
凡VAR1
意味着是常量字符串this is a constant
的名稱。
現在我掃描這個字符串是這樣的:
if(sscanf(sInput, "%*s %s %s", paramname, constantvalue) != 2)
//do something
當然,當我輸出paramname
和constantvalue
我得到的:
VAR1
this
但我想有constantvalue
遏制弦,直到兩個連續的空格中找到(所以它會包含一部分this is a constant string
)。
因此,因此我想:
sscanf(sInput, "%*s %s %[^()]s", paramname, constantvalue)
sscanf(sInput, "%*s %s %[^ ]s", paramname, constantvalue)
但沒有運氣。有沒有辦法通過sscanf()
實現我的目標?或者我應該實施另一種存儲字符串的方式嗎?
你*是否使用['sscanf'](http://en.cppreference.com/w/c/io/fscanf)?請記住''%[「'格式不是正則表達式格式,它只是列出一組*字符。 –
@JoachimPileborg不,我沒有,但它似乎最容易給我。 – moffeltje
我不認爲這可以用'sscanf'完成。 –