C++ 中 char* 中的子字符串,行尾带有 strtok
sub string in char* in c++ with strtok to the end of the row
我尝试在 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");
}
这是行中的文本"Show abcde fghij - klmnop qrstu"
这是变量数据:
teamAName = abcde
teamACity = fghij
teamBName = klmnop
teamBCity = qrs
我怎样才能修复我需要切入行的团队BCity。
我在Linux系统上工作。
我认为这就是你想要的。 鉴于您的示例,不完全确定。 它显然可以改进,也许您想使用更多的 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 %sn", teamAName);
printf("teamACity %sn", teamACity);
printf("teamBName %sn", teamBName);
printf("teamBCity %sn", teamBCity);
// do something with strings here?
}
相关文章:
- 如何在C 中不带有弦乐和strtok的情况下拆分字符串(提取单词)
- C++ 使用 Strtok 读取字符串字符时出错
- 拆分C++字符串(使用 strtok?),由逗号分隔,不使用外部库
- Strtok似乎没有修改输入字符串
- strtok():将字符串转换为char*
- 使用 strtok 查找子字符串
- strtok() - 为什么必须传递 NULL 指针才能获取字符串中的下一个标记
- Strtok():标记化字符串
- C++ 中 char* 中的子字符串,行尾带有 strtok
- 字符串上strtok()的C++用法
- 使用strtok复制字符串部分时出现内存错误
- 使用 strtok 在条件下拆分字符串
- 使用 strtok() 的错误字符串比较
- 比较 strtok 之后的字符串
- 使用 strtok 拆分字符串
- 将字符串添加到 vector<const char *> 会给 strtok() 带来奇怪的行为
- 如何使"strtok function"一次使用多个令牌字符串? 函数指针会解决这个问题吗?
- 将字符串分隔为分隔符strtok (c++)
- 尝试对字符串使用"strtok"时出错
- c++ strtok in函数将原始字符串值更改为参数