2015-06-22 79 views
3

一個衆所周知的scanf()功能的功能是可以傳遞的格式根據該格式進行掃描輸入。如何讀取字符串,直到兩個連續的空格?

對於我的情況,我似乎無法找到搜索thisthis文檔的解決方案。

我有一個字符串(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 

當然,當我輸出paramnameconstantvalue我得到的:

VAR1 
this 

但我想有constantvalue遏制弦,直到兩個連續的空格中找到(所以它會包含一部分this is a constant string)。

因此,因此我想:

sscanf(sInput, "%*s %s %[^()]s", paramname, constantvalue) 
sscanf(sInput, "%*s %s %[^ ]s", paramname, constantvalue) 

但沒有運氣。有沒有辦法通過sscanf()實現我的目標?或者我應該實施另一種存儲字符串的方式嗎?

+1

你*是否使用['sscanf'](http://en.cppreference.com/w/c/io/fscanf)?請記住''%[「'格式不是正則表達式格式,它只是列出一組*字符。 –

+0

@JoachimPileborg不,我沒有,但它似乎最容易給我。 – moffeltje

+0

我不認爲這可以用'sscanf'完成。 –

回答

3

scanf family of functions像你似乎做的是好簡單的解析,而不是更復雜的東西。

可以可能通過使用例如解決問題strstr找到註釋啓動"//",終止字符串那裏,然後刪除尾隨space

+1

好吧,這似乎回答我的問題,並感謝你的例子。讓我們建立! – moffeltje

相關問題