-2

If i have a file that has a table of 64*64 integers. (The first 64 will be row 0; the next 64 will be row 1, and so on..). How do I store that table into a 2D array. Here is my code

 #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {   
        ifstream infile;
        infile.open("table.txt");
        if (infile.fail())
        {
            cout << "could not open file" << endl;
            exit(6);
        }
        int array[63][63];      
        while (!infile.eof())
        {
            infile >> array[63][63];
        }
        cout << array[63][63] << endl;
        return 0;
    }

when this is executed I only get "1"

  • Every time your while loop iterates, you are assigning the infile result into array[63][63] rather than changing the x,y (or whatever indices) and populating the whole array. – K Richardson Oct 10 '16 at 19:48
  • 1
    For one, [this is wrong: `while (!infile.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). And, you always read the *same* element, overwriting with each iteration. Your output is a single value precisely because that is what your code does; one output value: `cout << array[63][63] << endl;`. I think you need to review the parts of the language you're learning, because there is quite a bit missing here. – WhozCraig Oct 10 '16 at 19:49
  • `infile >> array[63][63];` this goes out of bounds and is undefined behavior. – πάντα ῥεῖ Oct 10 '16 at 19:49
  • all is wrong in your code! first you don't create 64x64 but 63x63, then you're storing all the elements (with the end of file bug) to a location _outside_ this array (you never use any indexes to increment rows or columns!) – Jean-François Fabre Oct 10 '16 at 19:49

2 Answers2

1

I have a file that has a table of 64*64 integers (...) How do I store that table into a 2D array?

First of all, you have to declare an array of the right size (well, you should consider using a std::vector or a std::array instead, but you asked for a 2D array):

const size_t size = 64;
int array[size][size];

Then you have to assign each of its elements in a loop. In your code you repetedly write over element array[63][63], which is also outside the range you allocated, because you declared the array as int array[63][63]. Remember that array indeces start from 0, so if you allocate enough memory for, say, 63 ints, only the indeces from 0 to 62 are valid.

A possible way to accomplish this task is:

for ( size_t i = 0; i < size; ++i )
{
    for ( size_t j = 0; j < size; ++j )
    {
        if ( !(infile >> array[i][j]) )
        {
            std::cerr << "Too few elements were read from file\n";
            exit(7);
        }
    }
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
0

Rather than assigning the value from the file into array[63][63] you should assign it to one element and increment an index to populate the whole array:

int x = 0; int y = 0;
while (!infile.eof())
{
    infile >> array[x++][y];
    if (x > 63)
    {
         x = 0;
         y++;
    }
}

Something along these lines should work well.

Also, as commented above, the array needs to be initialized as int array[64][64].

K Richardson
  • 1,640
  • 1
  • 10
  • 14