我遇到了一个问题,它是从包含空格的单词和随机的新行中读取的。这里是我的代码:从文件中读取空格和换行的单词的C++
vector<string> _vecIgnoreWords;
vector<string> _vecHungerGames;
void readTextFile(char *fileNameHungerGames, vector<string>& _vecHungerGames){
ifstream fileInHungerGames;
string newline;
fileInHungerGames.open(fileNameHungerGames);
if(fileInHungerGames.is_open()){
while(getline(fileInHungerGames, newline)){
stringstream iss(newline);
while(iss){
iss >> newline;
if(!(isCommonWord(newline, _vecIgnoreWords))){
_vecHungerGames.push_back(newline);
cout << newline << endl;
}
}
}
fileInHungerGames.close();
}
呼叫主:
string fileName = argv[2];
string fileNameIgnore = argv[3];
char* p = new char[fileNameIgnore.length() + 1];
memcpy(p, fileNameIgnore.c_str(), fileNameIgnore.length()+1);
getStopWords(p, _vecIgnoreWords);
char* hungergamesfile_ = new char[fileName.length() + 1];
memcpy(hungergamesfile_, fileName.c_str(), fileName.length()+1);
readTextFile(hungergamesfile_, _vecHungerGames);
停止词无效:
void getStopWords(char *ignoreWordFileName, vector<string>& _vecIgnoreWords){
ifstream fileIgnore;
string line;
fileIgnore.open(ignoreWordFileName);
if(fileIgnore.is_open()){
while(getline(fileIgnore, line)){
_vecIgnoreWords.push_back(line);
}
}
fileIgnore.close();
return;
}
我的问题现在是,我对这个代码的输出最终像:
bread
is
is
slipping
away
take
我不是确定为什么我重复(是)和我使用字符串流时的空行?
我的输出应该是这样的:
bread
is
slipping
away
from
me
还略显不那么重要,但我while循环循环一次太多这就是为什么我有if(_vecHungerGames.size() == 7682)
是有办法解决的循环一旦太多这个循环?
文件例如:
bread is
slipping away from me
i take his hand holding on tightly preparing for the
请输入文件的样本添加到您的帖子。 –
档案是非常长的(整个饥饿游戏书之一),但这里是它的一部分的一个例子: –
面包是 从我身上滑落 我握住他的手紧紧准备 –