I have a file with a network map consisting of 0's and 1's. I want to read this map into a two-dimensional array. I have the following code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char map[22][22]; // 22 x 22 (r*c)
ifstream fin;
fin.open("netmap.txt");
char ch;
// populate array with contents of map file.
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 22; j++)
{
fin.get(ch);
map[i][j] = ch;
}
}
// output of populated array...
for (int i = 0; i < 22; i++)
{
for (int j = 0; j < 22; j++)
{
cout << map[i][j];
}
}
cout << endl;
}
The contents of netmap.txt are a 22x22 grid of 0's and 1's.
0110000000000000000000
1011000000000000000000
1100000000000000000000
0100010000000000000000
0000000111000000000000
0000001100000010000000
0000010000000000000000
0000110000000000000000
0000100000000100000000
0000100000110100000000
0000000001001000000000
0000000001001001000000
0000000000110000010000
0000000011000000000000
0000010000000000001000
0000000000010000100000
0000000000000001000000
0000000000001000000010
0000000000000010000100
0000000000000000001000
0000000000000000010001
0000000000000000000010
The output I get however is:
0110000000000000000000
1011000000000000000000
1100000000000000000000
0100010000000000000000
0000000111000000000000
0000001100000010000000
0000010000000000000000
0000110000000000000000
0000100000000100000000
0000100000110100000000
0000000001001000000000
0000000001001001000000
0000000000110000010000
0000000011000000000000
0000010000000000001000
0000000000010000100000
0000000000000001000000
0000000000001000000010
0000000000000010000100
0000000000000000001000
0000000000000000010001
0
It is as if the program were deciding to essentially terminate following that last number which is just the first number of the final line. I'm really confused as to why this is happening and any assistance in correcting (or explaining) the problem would be greatly appreciated. Thanks.