我试图解析一个名为 FileName 的文本文件,它看起来像:
KD JD 6s 5s 3c // no rank (king high)
AH Ks Js AD Ac // three of a kind
现在我想将它们存储到一个向量中(“//”之前的所有内容)。
int FileParsing(vector<Card> & v, char * FileName) {
ifstream ifs;
ifs.open(FileName);
if (!ifs.is_open()){
cout << "file cannot be opened." << endl;
} else {
string line;
while(getline(ifs, line)){
istringstream iss (line);
bool condition = CountWords(line); //dont worry about this method
ReadCardDefinitionStrings (condition, line, v);
}
}
return 0;
}
void ReadCardDefinitionStrings (bool condition, string line, vector<Card> & v){
istringstream iss (line);
string CardDefinitionStrings; //e.g. 2C, 3h, 7s, Kh
if (condition) {
while(iss>>CardDefinitionStrings){
if (CardDefinitionStrings == "//"){ //stop reading after it sees "//"
return;
}
Card c = TestCard(CardDefinitionStrings);
v.push_back(c);
}
}
}
我遇到的问题是:当它在 3c 之后看到“//”时,它会回到
while(getline(ifs, line)){
istringstream iss (line);
bool condition = CountWords(line);
ReadCardDefinitionStrings (condition, line, v);
}
但是这一次,行是空的(我希望它是:AH Ks Js AD Ac // 三种),这意味着这个循环运行一次而不做任何事情。下一次运行,line 将等于 AH Ks Js AD Ac // 三种。知道为什么会这样吗?