【发布时间】:2015-01-05 22:17:40
【问题描述】:
所以我已经搜索了很长一段时间,但目前没有发现任何可以解决我的问题的问题 我之前的问题中有一个乱七八糟的代码,有人可以尝试整理一下以使其正常工作或简单地重新制作它,我有 4 个不同的文本要读取的文件取决于它是什么,所以 switch 语句是我最好的主意,我打算保留这一点。仅仅添加这段代码是行不通的......而且无法弄清楚为什么不这样做。
ifstream QuestionFile;
int i = 0;
switch (x){
case 1:
QuestionFile.open("Topic1 Questions.txt", ios::app);
break;
case 2:
QuestionFile.open("Topic2 Questions.txt", ios::app);
break;
case 3:
QuestionFile.open("Topic3 Questions.txt", ios::app);
break;
case 4:
QuestionFile.open("Topic4 Questions.txt", ios::app);
break;
}
stringstream buffer;
buffer << QuestionFile.rdbuf();
string test = buffer.str();
size_t pos1 = 0;
size_t pos2;
if (!QuestionFile)
{
cout << "Cannot load file" << endl;
}
else
{
if (QuestionFile.peek() != ifstream::traits_type::eof()) {
while (!QuestionFile.eof())
{
pos2 = test.find("|", pos1);
questions[i] = test.substr(pos1, (pos2 - pos1));
cout << questions[i];
i++;
}
QuestionFile.close();
}
}
【问题讨论】:
-
你的编程语言是什么?
-
在我看来像 C++,它被标记为这样。
-
我将它标记为 C++,所以 helloflash 看不到该标记。