简体   繁体   English

从文件读取输入并填充数组以存储数据供以后使用

[英]Reading input from file and populating array to store data for later use

I'm trying to make multiple arrays of maximum size 10 that hold the contents of an input file. 我正在尝试创建多个包含输入文件内容的最大大小为10的数组。 Suppose the file is formatted like: 假设文件的格式如下:

1 3 2 4 0 5 8 -3
3 1 4 6 0 3
4 83 2 12 1 3
4 30 4 -2

Where each line cannot exceed more than 10 integers. 每行不能超过10个整数。

void read_input(ifstream &file)
{
    vector<array<int, 10> > main_array;

    while(!(file.eof()))
    {
        int idx = 0;
        string values;
        array<int, 10> part_array;

        getline(file, values);
        istringstream inStream;
        inStream.str(values);

        while((inStream >> part_array[idx++]).good() == true);
        main_array.push_back(part_array);

        file.ignore();
    }

    for(auto it = main_array.begin(); it != main_array.end(); it++)
    {
        for(auto& s: *it)
        {
            cout << s <<  " ";
        }
        cout << "\n";
    }
}

This gives me the output: 这给了我输出:

1 3 2 4 0 5 8 -3 0 6164240
1 4 6 0 3 5 8 -3 0 6164240
83 2 12 1 3 5 8 -3 0 6164240
30 4 -2 1 3 5 8 -3 0 6164240

Which is clearly wrong. 这显然是错的。 I know this may have to do with me iterating over array elements that aren't initialized, but how can I avoid this error? 我知道这可能与我迭代未初始化的数组元素,但我怎么能避免这个错误? Just as a note, it is essentially that I use arrays to hold the values at each line in the file. 就像一个注释,本质上我使用数组来保存文件中每行的值。 That is, one array must handle the integers in the first line; 也就是说,一个数组必须处理第一行中的整数; another in the second line... 另一个在第二行......

You can use 您可以使用

 vector<vector<int> > main_array;

instead of an array - then just read items from input and push them (push_back) into the "inner" vector - this way you won't have uninitialized array elements at the end in case when data row is shorter than 10 elements. 而不是数组 - 然后只是从输入中读取项目并将它们(push_back)推送到“内部”向量中 - 这样,如果数据行短于10个元素,则最后不会有未初始化的数组元素。

Full solution may look like this (I've kept some parts of your original code for consistency): 完整的解决方案可能看起来像这样(我保留了原始代码的某些部分以保持一致性):

using namespace std;

void read_input(ifstream &file)
{
    vector<vector<int> > main_array;

    string line;    //buffer for single line read from file 
    // reading part
    while(getline(file, line))
    {       
        stringstream inStream(line);
        vector<int> row;    //single row buffer
        int val = 0;
        while(inStream >> val)
        {
            row.push_back(val);
        }       
        main_array.push_back(row);
    }   

    //display part
    for(auto it = main_array.begin(), end = main_array.end(); it != end; ++it)
    {
        for(auto& s: *it)
        {
            cout << s <<  " ";
        }
        cout << "\n";
    }
}

Use this if you find it useful, 如果你发现它有用,请使用它,

void readData(ifstream &inf)
{
    string str;
    vector<vector<int>> intArr;

    while (getline(inf, str))
    {
        stringstream ss(str);
        int i;
        vector<int> a;
        while (ss >> i)
        {
            a.push_back(i);
        }
        intArr.push_back(a);
    }
}

You shouldn't use eof() to check end of file. 您不应该使用eof()来检查文件结尾。

std::array is a container that encapsulates constant size arrays., so by declaring with array<int, 10> there are already 10 integers in the array. std :: array是一个封装常量大小数组的容器。因此,通过使用array<int, 10>声明, array<int, 10>中已经有10个整数。 You need to use vector instead, and change your loop 您需要使用向量,然后更改循环

 while((inStream >> part_array[idx++]).good() == true);

to

 bool good = true;
 while(idx <10 ) {
     int v;
     if(!(inStream >> v).good() ) break;
     idx++;
     part_array.push_back(v); //now a vector
 }

You have used an array with 10 elements but you know there may be fewer than ten items. 您使用了包含10个元素的array ,但您知道可能少于10个项目。 It would be better to use a vector, and push_back what you have. 最好使用vector,并push_back你拥有的东西。

Aside from that, there are a variety of potential problems with the code. 除此之外,代码存在各种潜在问题。 If you send it a bad ifstream it gets stuck in a loop. 如果你发送一个糟糕的ifstream它会陷入循环。

while(!(file.eof())) is asking for trouble. while(!(file.eof()))要求麻烦。

Next, file.ignore(); 接下来, file.ignore(); then ignores the first items of each line. 然后忽略每行的第一项。 If you change to use a vector of vectors as people have said, use while(getline(file, values)) instead of using the eof check and remove the ignore it should work. 如果你改变为使用矢量向量,就像人们所说的那样,使用while(getline(file, values))而不是使用eof检查并删除它应该工作的ignore

Change this line 改变这一行

array<int, 10> part_array;

to

array<int, 10> part_array = {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM