行中的令牌不起作用失败 C++
token in line doesn't work fail c++
下面的函数作用于名为"_filePath"的文本文件,并尝试将其分割成由";"answers"分隔的小标记,如下所示:
[Mickey M;12034;911416313;M;01a;9001;NULL;0;13;12;0;CPP,C;MSC,3D;FEND,BEND,SEC;]
当试图将CPP,C
分离为小令牌时,它不会到达下一个令牌MSC,3D
,以此类推。我注意到函数甚至没有到达以if (tokNum==1)
开头的块。
我真的需要帮助,非常感谢你:D
void File::set() {
if (_filePath.is_open()) {
while (_filePath.getline(line, 250)) {
char *token; char *tokeng;
int tokNum = 0;
token = strtok(line, ";");
while (token != NULL) {
index = 0;
if (token == "NULL") token = NULL;
if (inputType == EMP){
if (tokNum == 0){
int numWord = seprateElements(token);
empKnoledge = new string[numWord];
tokeng = strtok(token, ",");
while(tokeng != NULL) {
empKnoledge[index] = tokeng;
index++;
tokeng = strtok(NULL, ",");
}
}
if (tokNum == 1){
int numWord = seprateElements(token);
empAfeild = new string[numWord];
tokeng = strtok(token, ",");
while(tokeng != NULL) {
empAfeild[index] = tokeng;
index++;
tokeng = strtok(NULL, ",");
}
}
if (tokNum == 2){
int numWord = seprateElements(token);
empPfeild = new string[numWord];
tokeng = strtok(token, ",");
while (tokeng != NULL) {
empPfeild[index] = tokeng;
index++;
tokeng = strtok(NULL, ",");
}
}
}
tokNum++;
token = strtok(NULL, ";");
}
numLine++;
}
}
getchar();
}
int seprateElements(char *line) { // check size of elements in line by counting ','
int count = 0;
while (*line++ != ' ') {
if (*line == ',') count++;
}
return count+1;
}
在c++中有很好的类和函数,如std::string
, std::istringstream
和std::getline
。
第一个,std::string
是您应该用于字符串的类。第二个类std::istringstream
是一个处理字符串而不是文件的输入流。最后,std::getline
函数可用于将流中的一行转换为std::string
对象,但它有一个可选参数,允许它使用特殊字符(例如分号)分隔字段。
使用这些函数,您可以执行
std::string line;
while (std::getline(_filePath, line))
{
// Put the line into an input string stream
std::istrimgstream linestream(line);
std::string name;
std::string id;
// ... all other fields in the line
std::string last_field; // or whatever you want to name it
// Now extract all the fields from the line
std::getline(linestream, name, ';');
std::getline(linestream, id, ';');
// ... all other fields
std::getline(linestream, last_field); // Last field, no separator needed
// Some fields contains multiple tokens separated by comma
// Example extracts token from the last field of the line
std::istringstream tokenstream(last_field);
std::string token;
while (std::getline(tokenstream, token, ','))
{
std::cout << "Extracted token '" << token << "'n";
}
}
在一个不相关的边注上,请尽量避免您自己的带下划线的符号,在许多情况下它们是保留的。
使用字符串标记拆分,请参见下面的代码。
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[]="Mickey M;12034;911416313;M;01a;9001;NULL;0;13;12;0;CPP,C;MSC,3D;FEND,BEND,SEC;";
char *pch = strtok (str,";,");
while (pch != NULL)
{
cout<<pch<<"n";
pch = strtok (NULL, ";,");
}
return 0;
}
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- 生成MRPT库时cmake配置失败
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- variadic模板中的模板参数推导失败