[英]While does it go through the while loop twice
I have trying parse a text file called FileName that looks like: 我试图解析一个名为FileName的文本文件,如下所示:
KD JD 6s 5s 3c // no rank (king high)
AH Ks Js AD Ac // three of a kind
Now I wanna store them into a vector (everything before "//"). 现在,我想将它们存储到向量中(“ //”之前的所有内容)。
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);
}
}
}
The problem I am getting is: when it sees "//" after 3c, it goes back to 我得到的问题是:当3c之后看到“ //”时,它返回
while(getline(ifs, line)){
istringstream iss (line);
bool condition = CountWords(line);
ReadCardDefinitionStrings (condition, line, v);
}
But this time, line is empty (I wanted it to be: AH Ks Js AD Ac // three of a kind), which means this loop runs one time without doing anything. 但是这一次,行是空的(我希望它是:AH Ks Js AD Ac //三种),这意味着此循环运行了一次却没有执行任何操作。 And the next run, line would be equal to AH Ks Js AD Ac // three of a kind.
在下一次运行中,线将等于AH Ks Js AD Ac //三种。 Any idea why this happens?
知道为什么会这样吗?
I think you forgot to put a lot of code up (card definitions and the like). 我认为您忘了编写很多代码(卡定义等)。 And, from what I could tell, you had a couple of redundant definitions in some places.
而且,据我所知,您在某些地方有两个多余的定义。
However, I've implemented what you want (the ability to parse a text file called FileName that looks like): 但是,我已经实现了您想要的(解析名为FileName的文本文件的功能):
KD JD 6s 5s 3c // no rank (king high)
AH Ks Js AD Ac // three of a kind
Assuming the input filestream has been initialized with the Filename: 假设输入文件流已使用文件名初始化:
ifstream ifs(FileName);
if (!ifs.is_open()) {
cout << "file cannot be opened." << endl;
}
else {
string line;
//getline reads up until the newline character
//but you want the characters up to "//"
while(std::getline(ifs, line)) {
//line has been read, now get rid of the "//"
string substr = line.substr(0,line.find("//"));
//assign the substr to an input stringstream
istringstream iss(substr);
//this will hold your card
//NOTE: Since I don't have your implementation of Card, I put it as a string instead for demonstration purposes
string card;
//keep reading until you've read all the cards in the substring (from line)
while (iss >> card) {
v.push_back(card);//store the card in the vector
cout << card << endl;//now output it to verify that the cards were read properly
}
cout << endl;//this is just for spacing - so it'll help us distinguish cards from different lines (in the output)
}
ifs.close();
}
Output: 输出:
KD
JD
6s
5s
3c
AH
Ks
Js
AD
Ac
Which is what you want. 你想要哪一个。 ;)
;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.