我编写了以下代码,经过最长的凝视并尝试了一系列不同的事情之后,我什么都没做。问题是,尽管“正确”,但屏幕上打印的文本并不包含我每次都打印的const字符串,例如“ date:”或“ task:”。
这是代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main() {
ifstream myFile("calend.txt");
vector<string> info;
string str;
while (getline(myFile, str, '-')) {
info.push_back(str);
if (info.size() > 1) {
cout << "date: " << info.at(0) << endl;
cout << "task: " << info.at(1) << endl;
info.clear();
}
}
myFile.close();
system("pause");
return 0;
}
这是文本文件(calend.txt):
15/05-checkpoint IART
18/05-checkpoint COMP
22/05-SDIS
25/05-apresentacao PPIN
27/05-IART
28/05-apresentacao LPOO
28/05-teste PPIN
01/06-LBAW
05/06-COMP
08*14/06-PPIN
最后是输出:
date: 15/05
task: checkpoint IART
18/05
date: checkpoint COMP
22/05
task: SDIS
25/05
date: apresentacao PPIN
27/05
task: IART
28/05
date: apresentacao LPOO
28/05
task: teste PPIN
01/06
date: LBAW
05/06
task: COMP
08*14/06
如您所见,前两行已正确打印,但随后跳过了const字符串。提前致谢。