简体   繁体   English

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

[英]token in line doesn't work fail c++

The following function works on text file called "_filePath" and tries to cut it to little tokens separate by ";" 以下函数适用于名为“ _filePath”的文本文件,并尝试将其切成由“;”分隔的小标记。 and "," like this: 和“,”是这样的:

[Mickey M;12034;911416313;M;01a;9001;NULL;0;13;12;0;CPP,C;MSC,3D;FEND,BEND,SEC;]

When trying to separate CPP,C into little token, it doesn't reach the next token which is MSC,3D and so on. 当尝试将CPP,C分离为少量令牌时,它不会到达下一个令牌,即MSC,3D等。 I notice that the function doesn't even reach the block beginning with if (tokNum==1) . 我注意到该功能甚至没有到达以if (tokNum==1)开头的块。

I really need HELP, thank you very much :D 我真的需要帮助,非常感谢: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++ != '\0') {
        if (*line == ',')   count++;
    }
    return count+1;
}

In C++ there is nice classes and functions, such as std::string , std::istringstream and std::getline . 在C ++中,有很好的类和函数,例如std::stringstd::istringstreamstd::getline

The first, std::string is the class you should use for strings. 第一个std::string是您应该用于字符串的类。 The second class, std::istringstream , is an input stream that works on strings instead of files. 第二类std::istringstream是输入流,它处理字符串而不是文件。 Finally, the std::getline function can be used to get a line from a stream into a std::string object, but it has an optional argument that allows it to separate fields on a special character, for example the semicolon. 最后,可以使用std::getline函数将一行从流中获取到std::string对象中,但是它具有一个可选参数,可用于分隔特殊字符(例如分号)上的字段。

Using these functions you could to something like 使用这些功能,您可以像

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

On an unrelated side-note, please try to avoid your own symbols with leading underscore, in many cases they are reserved . 在不相关的旁注中,请避免使用带下划线的您自己的符号, 在许多情况下,它们是保留的

Use String token Split , see following code do for you. 使用String令牌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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM