2014-04-18 86 views
0
#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 
char *tokenstring = "first,25.5,second,15"; 
int result, i; 
double fp; 
char o[10], f[10], s[10], t[10]; 
void main() 
{ 
    result = sscanf(tokenstring, "%[^','],%[^','],%[^','],%s", o, s, t, f); 
    printf("%s\n %s\n %s\n %s\n", o, s, t, f); 
    fp = atof(s); 
    i = atoi(f); 
    printf("%s\n %lf\n %s\n %d\n", o, fp, t, i); 
} 

上面的代碼不適用於'\ t',爲什麼?它的工作原理爲this 我使用VC6.0sscanf字符串格式說明符不適用於' t'

不工作

char *tokenstring = "first\t25.5\tsecond\t15"; 



    result = sscanf(tokenstring, "%[^'\t'],%[^'\t'],%[^'\t'],%s", o, s, t, f); 
+0

首先,不要將單個字符括在引號中。空格的格式應該是''%[^ \ r \ n \ t]'' –

+1

''%[^'\ t'],%[^'\ t']%s,%[^'\ t'],%s「' - >''%[^ \ t]%[^ \ t]%[^ \ t]%s」' – BLUEPIXY

+0

另外,你有一個''%s「'在那裏。 –

回答

3

看看你的格式是匹配:

"%[^'\t'],%[^'\t'] 
^ ^^ 
\  | \- match a literal comma 
    \ | 
    \---+- match a sequence not containing tab or ' (single quote), up to the next 
      tab or single quite. 

所以第一%[..]一切相匹配並沒有包括在輸入第一個選項卡,然後試圖匹配一個逗號,這與選項卡不匹配,因此失敗。

最簡單的解決方法是用空格替換字符串中的逗號,它將跳過空格(包括製表符)。使用標籤將做同樣的事情,但會讓人混淆,以爲你想匹配一個標籤,而不是跳過空白:

sscanf(tokenstring, "%[^\t] %[^\t] %[^\t]%s", o, s, t, f); 

注意,也可能不希望在特殊對待'字符匹配,除非你想讓他們失敗。

現在,如果你想用你的分離只是標籤(不是任何空格),你需要使用片圖案:

sscanf(tokenstring, "%[^\t]%*1[\t\]%[^\t]%*1[\t]%[^\t]%s", o, s, t, f); 

模式%*1[\t]將在輸入精確匹配一個標籤,而不是存儲在任何地方。

這導致你可能已經用你的第一個(逗號爲主)scanf函數注意到另一個問題 - 像%[^,]%[^\t]模式不匹配一個空字符串 - 如果輸入的下一個字符是,(或\t在第二種情況下),scanf將簡單地返回而不匹配任何內容(或任何以下模式),而不是存儲空字符串。另外,如果你的任何字符串對於數組來說太長,你會溢出並且崩潰(或者更糟糕)。所以,無論你使用一個scanf函數%s%[模式到緩衝區中,你應該總是指定的緩衝區大小:

sscanf(tokenstring, "%9[^,],%9[^,],%9[^,],%9s", o, s, t, f); 

現在,而不是崩潰或損壞的東西,當輸入太長,sscanf通話將只匹配字段的前9個字符,並返回字段的其餘部分尚未讀取。

+0

'%* 1 [\ t]'等於'\ t',對吧? –

+0

'sscanf(tokenstring,「%[^ \ t]%* 1 [\ t]%[^ \ t]%* 1 [\ t]%[^ \ t]%s」,o,s,t,f );' –

2

當您使用逗號分隔字段,你必須一個,添加到格式字符串,從而跳過它。同樣爲\t

#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 

void test1() 
{ 
    char *tokenstring = "first,25.5,second,15"; 
    int result, i; 
    double fp; 
    char o[10], f[10], s[10], t[10]; 

    // ----------------------------------------------------------- 
    // make sure you add a , between the string format specifiers 
    // ----------------------------------------------------------- 
    result = sscanf(tokenstring, "%[^,],%[^,],%[^,],%s", o, s, t, f); 
    printf("%s\n %s\n %s\n %s\n", o, s, t, f); 
    fp = atof(s); 
    i = atoi(f); 
    printf("%s\n %lf\n %s\n %d\n", o, fp, t, i); 
} 

void test2() 
{ 
    char *tokenstring = "first\t25.5\tsecond\t15"; 
    int result, i; 
    double fp; 
    char o[10], f[10], s[10], t[10]; 

    // ----------------------------------------------------------- 
    // make sure you add a \t between the string format specifiers 
    // ----------------------------------------------------------- 
    result = sscanf(tokenstring, "%[^\t]\t%[^\t]\t%[^\t]\t%s", o, s, t, f); 
    printf("%s\n %s\n %s\n %s\n", o, s, t, f); 
    fp = atof(s); 
    i = atoi(f); 
    printf("%s\n %lf\n %s\n %d\n", o, fp, t, i); 
} 

void main() 
{ 
    test1(); 
    test2(); 
}