how can i count the number of columns of a 2D array?

I'm trying to count the number of rows and columns of a 2D array. The elements for the array are in a different text file and I have to import the file.

1
2
3
4
5
6
7
1 2 4 5 6 7
10 6 8 1 0 -1
2 8 89 8 9 6
101 654 325 1001 789 123
789 36 36 10 25 14
-1 -1 -1 0 1 1
74 56 87 12 74 96

Above are the 2D array elements which are stored in numbers.txt file. Now know I know it's 7x6 array. I just want to find a way to measure that. This is what I did so far;

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
#include <iostream>
#include <fstream>
using namespace std;
int main()
{	
	ifstream fin("numbers.txt");
	
	char buffer1[100] = {'\0'};
	
	int numofrows = 0;	
	while(!(fin.eof()))
	{
		fin.getline(buffer1,30);
		numofrows++;
	}
	cout << "Number of rows : " << numofrows << endl;
	
	char buffer2 = '\0';
	
	int numofcol = 0;
	while(buffer2 != '\n')
	{
		fin.get(buffer2);
		if(buffer2 != ' ')
		{
			numofcol++;
		}
	}
	cout << "Number of columns : " << numofcol;
}

It's giving the output for rows correctly but something is wrong for column.
Topic archived. No new replies allowed.