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

Losing the last token from every line of input read in

本文关键字:输入 一行 令牌 最后一个      更新时间:2023-10-16

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

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

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

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

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

我该如何解决这个问题?谢谢。

循环后添加:

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