jeudi 8 octobre 2020

Reading a txt file into an int 2D array

I'm trying to read from a text file that contains integers into a 2D array of rows and columns. As a debug statement I am printing the values to the screen. However, I cannot understand why I am not reading the very first value of 0. This is what the text file looks like:

0 1 -1 -1

-1 -1 -1 -1

-1 -1 2 3

-1 -1 -1 -1

-1 -1 -1 -1

-1 -1 -1 -1

-1 -1 -1 -1

-1 -1 -1 -1

-1 -1 -1 -1

-1 -1 -1 -1

Here is my code:

int  TRS[10][4];

int DFAcounter = 0;
        ifstream fin("trs.txt", ios::in);
        int row = 0, col = 0;  // row and col numbers
    
    
    
        // Read in the file into TRS
           while (fin >> row)                      // next line of file
{
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 4; j++) {
            fin >> TRS[row][col];
            cout << TRS[row][col];
            col++;
        }
        cout << endl;
    }
    row++;
}

But my cout output looks like this:

1-1-1-1

-1-1-1-1

-123-1

-1-1-1-1

-1-1-1-1

-1-1-1-1

-1-1-1-1

-1-1-1-1

-1-1-10

Any help is appreciated

Aucun commentaire:

Enregistrer un commentaire