我目前正在开发一个程序,该程序从文件中读取每一行并使用特定分隔符从该行中提取单词。
所以基本上我的代码看起来像这样
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argv, char **argc)
{
ifstream fin(argc[1]);
char delimiter[] = "|,.\n ";
string sentence;
while (getline(fin,sentence)) {
int pos;
pos = sentence.find_first_of(delimiter);
while (pos != string::npos) {
if (pos > 0) {
cout << sentence.substr(0,pos) << endl;
}
sentence =sentence.substr(pos+1);
pos = sentence.find_first_of(delimiter);
}
}
}
但是我的代码没有读取该行的最后一个单词。例如,我的文件如下所示。你好世界
程序的输出只有单词 "hello" 而不是 "world" 。我使用 '\n' 作为分隔符,但为什么它不起作用?
任何提示将不胜感激。