Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何逐行将文件读入数组c++;?_C++_Arrays_File Io - Fatal编程技术网

C++ 如何逐行将文件读入数组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

我已经写了这段代码,我读的文件包含3个选择题。该程序运行良好,我可以存储答案,但有一个问题。我需要在每次编译时将问题的顺序随机化。唯一的方法是读取数组中的文件。我似乎不知道该怎么做。感谢您的帮助。我是c的新手++

#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());
下面是一个演示如何使用这些标准图书馆设施的示例