2d Array from file

Hey guys im very new to C++ currently in university studying it but i am no where as good as i am at C#, well ive been trying to read in a txt file that has the format 9x9
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0

i want to then run through the text file and store them all in a 2d array and my best guess is by a for loop and for it to be printed into a output file in the exact same 9x9 format.
It would be great if someone could show me how to do so and point me in the direction of more help. thank you :)
Bump
arrays are not the most convenient data structures in C++, you'd be better off using a vector of vectors, or a real matrix class..

But it's certainly possible, in a variety of ways.. for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

int main()
{
    const int ROWS = 9;
    const int COLS = 9;
    int a[ROWS][COLS] = {};

    std::ifstream file("test.txt");

    for(int row = 0; file && row < ROWS; ++row)
        for(int col = 0; file >> a[row][col] && col < COLS; ++col)
            if(file.get() != ',') break;

    for(int row = 0; row < ROWS; ++row) {
        for(int col = 0; col < COLS; ++col)
            std::cout << a[row][col] << ' ';
        std::cout << '\n';
    }
}
Topic archived. No new replies allowed.