行中的令牌不起作用失败 C++

token in line doesn't work fail c++

本文关键字:失败 C++ 不起作用 令牌      更新时间:2023-10-16

下面的函数作用于名为"_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::istringstreamstd::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;
}