C++ 如何逐行将文件读入数组c++;?
我已经写了这段代码,我读的文件包含3个选择题。该程序运行良好,我可以存储答案,但有一个问题。我需要在每次编译时将问题的顺序随机化。唯一的方法是读取数组中的文件。我似乎不知道该怎么做。感谢您的帮助。我是c的新手++C++ 如何逐行将文件读入数组c++;?,c++,arrays,file-io,C++,Arrays,File Io,我已经写了这段代码,我读的文件包含3个选择题。该程序运行良好,我可以存储答案,但有一个问题。我需要在每次编译时将问题的顺序随机化。唯一的方法是读取数组中的文件。我似乎不知道该怎么做。感谢您的帮助。我是c的新手++ #include <iostream> #include <string> #include <fstream> using namespace std; int main() { char c; char d; str
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
char c;
char d;
string line_;
ifstream file_("mpchoice.txt");
if (file_.is_open())
{
while (getline(file_, line_))
{
cout << line_ << '\n';
}
file_.close();
}
cout << "What is your response for number 1\n";
cin >> c;
if (c == 'A')
cout << "That's wrong\n";
cout << "What's your response for the second question\n";
cin >> d;
if (d == 'A'){
cout << "That's correct\n";
}
else
cout << "That's wrong\n";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符c;
chard;
弦线;
ifstream文件(mpchoice.txt);
如果(文件是打开的())
{
while(getline(文件,行))
{
cout您可以首先从
标题将每一行放入std::vector
():
ifstream file_("mpchoice.txt");
vector<string> lines;
if (file_.is_open())
{
while (getline(file_, line_))
{
cout << line_ << '\n';
lines.push_back(line_);
}
file_.close();
}
random_shuffle(lines.begin(), lines.end());
下面是一个演示如何使用这些标准图书馆设施的示例