1

I am having trouble reading numbers from a file into a 2d array in c++. It reads the first row just fine but the rest of the rows are populated with 0's. I have no idea what I'm doing wrong.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int myarray[20][20];

    int totRow = 20, totCol = 20, number, product, topProduct = 0, row, col, count;
    char element[4];

    ifstream file;

    file.open( "c:\\2020.txt" );

    if( !file )
    {
        cout << "problem";
        cin.clear();
        cin.ignore(255, '\n');
        cin.get();

        return 0;
    }

    while( file.good())
    {
        for( row = 0; row < totRow; row++ )
        {
            for( col = 0; col < totCol; col++ )
            {
                file.get( element, 4 );
                number = atoi( element );
                myarray[row][col] = number;
                cout << myarray[row][col] << " ";
            }
            cout << endl;

        }
        file.close();
    } 
3
  • 4
    what is the structure of 2020.txt ? Commented Oct 25, 2012 at 19:52
  • You could probably replace your entire while loop with std::copy_n( std::istream_iterator<int>( file ), 20*20, &myarray[0][0] );. Commented Oct 25, 2012 at 19:59
  • @trumpetlicks - No, that will work even if he has newlines. - ideone.com/u3Ww8P Commented Oct 25, 2012 at 20:01

2 Answers 2

3

If there are only numbers in your file, you can just read them with the >> operator. Change your inner loop to:

for( col = 0; col < totCol; col++ )
{
    file >> myarray[row][col];
    cout << myarray[row][col] << " ";
}

The problem with file.get() is, it doesn't read beyond newline \n. See: std::basic_istream::get

Sign up to request clarification or add additional context in comments.

2 Comments

@trumpetlicks No, this doesn't matter. The >> operator skips whitespace before reading the integer.
@trumpetlicks Look at std::basic_istream::operator>>
2

You're closing the file inside the while loop:

while( file.good())
    {
        for( row = 0; row < totRow; row++ )
        {
            for( col = 0; col < totCol; col++ )
            {
                file.get( element, 4 );
                number = atoi( element );
                myarray[row][col] = number;
                cout << myarray[row][col] << " ";
            }
            cout << endl;

        }
        file.close();   // <------ HERE
    } // end of while loop is here

You obviously can't read from a closed stream. Now, because you're trying to read all the data in the first iteration of the while loop, this doesn't seem to be your immediate problem. Note however, that the stream can still be good() even after you've read all the meaningful data (for example if there's a traling new-line character) and in that case, you'll enter the loop for the second time. That's a bug.

3 Comments

While not good form, Im not sure this is his ultimate problem.
@trumpetlicks My best guess, unless OP posts a sample input.
@jrok But outside the reading loop ;-) When the file is closed, he's already gone over the whole myarray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.