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

token in line doesn't work fail c++

以下函数适用于名为“_filePath”的文本文件,并尝试将其切割成由“;”分隔的小标记和“,”像这样:

[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++ != '[=10=]') {
        if (*line == ',')   count++;
    }
    return count+1;
}

在 C++ 中有不错的 类 和函数,例如 std::string, std::istringstream and std::getline.

第一个,std::string is the class you should use for strings. The second class, std::istringstream, is an input stream that works on strings instead of files. Finally, the 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";
    }
}

在不相关的旁注中,请尽量避免使用带前导下划线的符号,in many cases they are reserved

使用 String token Split ,请看下面的代码。

#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;
}