2013-12-18 86 views
0

我儘量貓一個字符串,在在C的char * ++ 類型,這是我的代碼子字符串的strtok到行結束

void DBManager::printMatched(char* line, char* fileName) 
{ 
    line = strtok(line,"Show"); 
    char* teamAName = strtok(line," "); 
    char* teamACity = strtok(NULL,"-"); 
    char* teamBName = strtok(NULL," "); 
    char* teamBCity = strtok(NULL,"\n"); 
} 

,這是行的文本 「顯示ABCDE FGHIJ - klmnop qrstu」

這是可變數據:

teamAName = abcde 
teamACity = fghij 
teamBName = klmnop 
teamBCity = qrs 

如何解決teamBCity我需要削減到該行的內找到。

我在Linux系統上工作。

+0

看起來問題是在調用例程中。如何調用printMatched()? – chux

+0

當我打電話給functuin有\ n在行的末尾,但在行= strtok(行,「顯示」); \ n正在關閉.. – royb

+0

請顯示使用'printMatched()'並打印'teamBCity'的代碼。我懷疑問題在於如何生成'line'的參數或者如何打印'teamBCity'。 – chux

回答

1

我認爲這是你想要的。不完全確定給你的例子。它顯然可以改進,也許你想使用更多的C++ ISH設施?

void DBManager::printMatched(char* line, char* fileName) 
{ 
    char* linecpy = strdup(line); 

    char* dummy  = strtok(linecpy," "); 
    char* teamAName = strtok(NULL," "); 
    char* teamACity = strtok(NULL," "); 
      dummy  = strtok(NULL," "); 
    char* teamBName = strtok(NULL," "); 
    char* teamBCity = strtok(NULL," \n"); 

    printf("teamAName %s\n", teamAName); 
    printf("teamACity %s\n", teamACity); 
    printf("teamBName %s\n", teamBName); 
    printf("teamBCity %s\n", teamBCity); 

    // do something with strings here? 
}