读入的每一行输入都丢失最后一个令牌

时间:2018-09-01 01:48:25

标签: c++

我正在尝试编写一个程序,该程序从文件中读取代码行作为字符串,然后将该字符串分成每个逗号分隔的标记。 (这是一个逗号分隔的文件)。

它在大多数情况下都有效,但是我在每一行上都丢失了最后一个令牌。

//This is function splits input strings into tokens. It takes in an input 
string
//and a delimiter character as parameters.
vector<string> tokenize(string inputString, char delimiter) {

    //the store the delimiter in local variable
    char newDelimiter = delimiter;

    //store the input string in local variable
    string newString = inputString;

    //vector of strings to store tokens.
    vector<string> tokenVector;

    //temp variable for making tokens
    string temp;

    //iterate through the string...

    for (int i = 0; i < newString.size(); ++i) {

        //...checking for specified delimiter.
        //If not present, add current character to temp string.
        if (newString.at(i) != newDelimiter) {
            temp += newString.at(i);
        }
        //if present, add temp string to vector and reset temp variable
        else
        {
            tokenVector.push_back(temp);
            temp = "";
        }
    }

    return tokenVector;
}

int main() {

    //string variable input will store each line of input
    string input;

    //use getline initially to "skip" the header line
    getline(cin, input);

    //this vector will be used to store each input lines string tokens
    vector<string> stringTokens;

    //this while loop will execute as long as cin is reading lines from the 
    file
    while (getline(cin, input)) {

        //print out each original line for testing purposes
        cout << input << endl;

        //split the string into tokens around commas
        stringTokens = tokenize(input, ',');

        //TEST OUTPUT
        for (int j = 0; j < stringTokens.size(); ++j) {
            cout << stringTokens.at(j) << " ";
        }
        cout << endl;
    }

    return 0;
}

输出的示例行:第二行缺少THere。第一行只是原始字符串,第二行是输出:

1,000010007,01,XX,0,0,0,0,0,0,0,01 - XXXXX,XXXXXXXXXX,0,0,0,0,0,0,0,0,0,0,0

1 000010007 01 XX 0 0 0 0 0 0 0 01 - XXXXX XXXXXXXXXX 0 0 0 0 0 0 0 0 0 0

该如何解决?谢谢。

1 个答案:

答案 0 :(得分:0)

在循环之后添加:

if (temp != "")
{
    tokenVector.push_back(temp);
}

最后一个元素。

vector<string> tokenize(string inputString, char delimiter) {

    ...Code...

    for (int i = 0; i < newString.size(); ++i) {
        ...Code...
    }

    if (temp != "") {
        tokenVector.push_back(temp);
    }

    return tokenVector;
}