Reading from File into 2D array issue

I'm attempting to read from an already created file that is 5 x 5 into a 2D array. In my program I've tried both an eof() method and a boolean way to determine the end of the file. The boolean way did actually reading something into the array, but the numbers are totally incorrect from what's in the file. The eof() method doesn't even seem to read anything in.

The file I'm reading in looks like:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
11 18 25 2 9


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>

void readSquare(int[][5], const int, const int);

using namespace std;


int main() {
    
    const int ROW = 5;
    const int COL = 5;
    int magicArray[ROW][COL];
    
    readSquare(magicArray, ROW, COL);
        
    return 0;
}

void readSquare(int magicArray1[][5], const int ROW1, const int COL1)
{
    ifstream fileInput;
    
    fileInput.open("/Users/jeffreyaweimer/Desktop/Magic Squares 2/Prog2Input.txt");
    
    bool print = true;
    
    while (print) //!fileInput.eof()
    {
        for (int i = 0 ; i < ROW1; i++)
        {
            fileInput >> magicArray1[i][0] >> magicArray1[i][1] >> magicArray1[i][2] >> magicArray1[i][3] >> magicArray1[i][4];
        }
        
        print = false;
    }
    
    fileInput.close();
    
   
    for (int i = 0; i < ROW1; i++)
    {
        for (int j = 0; j < COL1; j++)
        {
            cout << magicArray1[i][j] << " ";
        }
    }
    
    
}


What gives?
If you look at line 11, your const indicates you're reading in five rows. What you've copied from your file, however, only has four row, so on the last iteration of your loop on line 30, you're reading in garbage.

Edit: I'd also recommend that if you're going to pass both rows and columns as parameters as you do in your readSquare function, that you still use nested for loops to read in your file. instead of using magic numbers like you do on line 32.
Last edited on
@Keene, You're right I was missing a line in the input file. However, I added that and what my program is reading in is still garbled. I also updated the for loop from line 32? Further suggestions...

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>

void readSquare(int[][5], const int, const int);

using namespace std;

int main() {
    
    const int ROW = 5;
    const int COL = 5;
    int magicArray[ROW][COL];
    
    
    readSquare(magicArray, ROW, COL);
    
    //printSquare(magicArray, ROW, COL);
    

    return 0;
}

void readSquare(int magicArray1[][5], const int ROW1, const int COL1)
{
    ifstream fileInput;
    
    fileInput.open("/Users/jweim/Desktop/Magic Squares 2/Prog2Input.txt");
    
    bool print = true;
    
    while (print) //!fileInput.eof()
    {
        for (int i = 0 ; i < ROW1; i++)
        {
            for (int j = 0 ; j < ROW1; j++)
            {
                fileInput >> magicArray1[i][j];
            }
        }
        
        print = false;
    }
    
    fileInput.close();
    
    for (int i = 0; i < ROW1; i++)
    {
        for (int j = 0; j < COL1; j++)
        {
            cout << magicArray1[i][j] << "\n ";
        }
    }
    
    
}
My only other suggestion would be that the file path you're providing is incorrect. It's typically good practice to check to see if the file opened successfully. If it is indeed because the file didn't open successfully, you're running into a unique problem in that you're not using .eof(). If you were, you'd just get an infinite loop and no output. I'd suggest putting the file in the same directory as your .cpp file, and referring to it simply as fileInput.open("file.txt");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if ( !fileInput.is_open() ) return; //if the file didn't open correctly, quit the function

while ( !fileInput.eof() ) //this is a viable option now
    {
        for (int i = 0 ; i < ROW1; i++)
        {
            for (int j = 0 ; j < COL1; j++) //you had the incorrect const here originally.
            {
                fileInput >> magicArray1[i][j];
            }
        }
    }

    fileInput.close();

    for (int i = 0; i < ROW1; i++)
    {
        for (int j = 0; j < COL1; j++)
        {
            cout << magicArray1[i][j] << " "; //space between columns
        }
        cout << endl; //newlines for rows
    }
Last edited on
You don't need to pass your constants into the function if you just declare them outside of main, they then become global to your program and don't need to be passed.
Topic archived. No new replies allowed.