行中的令牌没有工作失败c ++

时间:2015-06-03 11:11:40

标签: c++ loops line token

以下函数适用于名为" _filePath"的文本文件。并试图通过&#34 ;;"将它切割成小标记。和","像这样:

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

2 个答案:

答案 0 :(得分:2)

在C ++中有很好的类和函数,例如std::stringstd::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";
    }
}

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

答案 1 :(得分:0)

使用字符串标记拆分,请参阅以下代码。

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