Q:

Loading multidimentional array from a file

I wanna load a multidimentional array from a file, I have this code:
std::vector<std::vector<int>> matrix;
for (int r = 0; r < cols; r++)
{
    std::vector<int> row;
    for ( int c = 0 ; c < cols ; c++ )
    {
        int temp;
        if ( fin >> temp )
        {
            std::cout << temp;
            row.push_back(temp);
        }
    }
    matrix.push_back(row);
}

Cols variable is fine, the nested loop is called 9 times if I have 3x3 array, so this works as expected...However it seems that the file cannot read single integer (fin >> temp). Fin is the file handler. What's wrong?
File content:
0 1 1
0 0 1
1 1 1

The whole code:
std::vector<std::vector<int>> foo()
{
    std::string filename;
    std::cout << "Filename: ";
    std::cin >> filename;

    std::vector<std::vector<int> > matrix;
    std::ifstream fin(filename);
    if(!fin) {
        std::cout << "Error";
        exit(EXIT_FAILURE);
    }

    std::string line;
    int cols = 0;
    if(fin.is_open()){
        while(!fin.eof()){
            std::getline(fin,line);
            cols++;
        }
    }
    for (int r = 0; r < cols; r++)
    {
        std::vector<int> row;
        for ( int c = 0 ; c < cols ; c++ )
        {
            int temp;
            if ( fin >> temp )
            {
                std::cout << temp; // displays nothing
                row.push_back(temp);
            }
                        std::cout << temp; // displays some crap like -84343141
        }
        matrix.push_back(row);
    }
    std::cin >> filename; // to stop execution and see the results
    return matrix;
}

A:

You are doing a first pass through the file to determine the size, but you are going to need to reopen the file after reading it the first time.  Otherwise, there is no data left in the file to read.

