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.

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.

Remember that a newline is a character, so every time you hit the end of the line, you are eating up a character that is not a 0 or a 1 and storing it in the array, so your for-loop eventually ends before all of the data is all read in. When you originally displayed your array, you didn't have a line return between rows, but they still displayed. That's because the newlines in the matrix were being outputted. If you get something other than a 0 or 1, you can discard it and get the next character, or you can use the >> operator instead of get . The code below gets rid of the newlines in the matrix and adds a newline after each row when displaying and reads the whole array in. Note the display sentence when you are reading in the data and you hit a character that isn't 0 or 1.

#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);

                      if (ch != '0' && ch != '1')
                      {
                           cout << "Not a zero or one.  Try again.\n";
                           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;
	}


	cout << endl;
}
commented: Absolutely great! =) +4

Worked perfectly. I thought it might have been a newline issue, but wasn't sure how to handle it.

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.