我尝试在c ++中捕获一个键入char *的字符串 这是我的代码
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 :(得分: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?
}