reading from text file and fill into array

hello everybody,
well..i have a text file as an array 4*4 like this

0 0 1 0
1 0 1 1
1 1 0 1
0 1 1 0

and i want to read this file and fill an array like arr[4][4].please help
Last edited on
Do you know how to use file streams? If not go to the tutorial and read the file IO section.
yes i know how to use it,but for the moment i want how to put the read text into the array....thanx for your quick reply
OK. Just create a nested for loop. The internal for loop will fill rows, and the outer will fill columns... or whatever you wanna do. As long as you haven't reached eof, you read in an integer and store it into the current slot in the array.
1
2
3
4
5
6
7
8
int matrix [4][4];
for (int row = 0; row < 4; row++)
{
    for (int col = 0; col < 4; col++)
    {
        file >> matrix[row][col];
    }
}

Something like that. That code presumes you will not reach eof which is a... risky... assumption to make. But I'm sure you know how to detect eof, right?
thank you tummychow but when i want to verify the array is that was filled correctly i put this code

1
2
3
4
5
for(int i=0;i<4;i++)
           { for(int j=0;j<4;j++)
              { printf("case[%d][%d]=%s \n",i,j,matrix[i][j]);
              }
           }


but i don't have any thing shown :(((

can you give me a solution??
Topic archived. No new replies allowed.