file to 2d array

Who can help me with an idea:
I have file with 2d array and need to read it to int array[][]. I don't know the size of array it can variate.
this is my array:
0 0 0 0 1 0 0 1
0 0 1 0 1 0 0 1
0 1 0 0 1 0 0 1
0 1 1 0 1 0 0 1
1 1 0 0 1 0 0 1
1 1 1 0 1 0 0 1
0 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0
1 1 0 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 1
1 1 1 1 1 1 1 0
0 0 1 0 0 0 1 1
0 0 0 0 0 0 1 1
0 1 0 0 0 0 1 0
0 1 1 0 0 0 1 0
0 1 0 0 0 0 1 1
0 1 1 0 0 0 1 1
1 1 0 0 0 0 1 1
0 0 0 1 1 1 1 0
0 1 1 1 1 1 1 0
I don't know the size of array it can variate.

Does that mean you don't know the number of rows, but there will always be eight columns, or can the number of columns vary as well?
rows and columns can variate. this is just one of examples.
Do you know the maximum number of rows and columns?
If not, you're not going to be able to use a simple 2d array.

You might get some ideas from this thread:
http://www.cplusplus.com/forum/beginner/128546/
Last edited on
Do you know the maximum number of rows and columns?

no i don't know the max size of rows and columns, I just can assume in won't be more than [30][20].
But can we read somehow element by element until we get nextline(\n)?
I just can assume in won't be more than [30][20].

Then you're making an assumption about the max sizes.

But can we read somehow element by element until we get nextline(\n)?

See the link I previously provided for reading a line at a time, then parsing the columns out of the line.
I didn't get how you count and delimited columns, could you please show me where in parse_data_row() can I count columns?
You don't need to count columns. In parse_data_row (from the previously linked post), row is a vector. Simply call row.size() to get the number of columns in a particular row.
Where can I call on row.size()?
it is always get =1, in instead of 8.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool parse_data_row(const string & line, vector<ROW> & vdata)
{	stringstream iss(line);
	string cell;
	double val;
	ROW row;

	while (!iss.eof())
	{   // good status, get one cell
		if (!getline(iss, cell, ';'))
			return false;  // some error 
		stringstream convertor(cell);
		convertor >> val;		// Input double from cell
		row.push_back (val);	// Add value to row
	}
	vdata.push_back (row);		// Add row to vector of rows


cout<<endl<<"size= "<<row.size()<<endl;


	return true;  // end of line
}
Take the ';' out of line 9. That was specific to the other poster's file format. Your columns are separated by spaces.

In your case val should be an int. No need for a double. The double was also specific to the other poster's file format.

Your ROW should also be a vector of ints.


At line 8,
if (!getline(iss, cell, ';'))
the delimiter is specified as a semicolon ';'
However, the sample data in the opening post has no semicolons.

You should change the delimiter to whatever is separating the items in your data file, probably a space. Or simplify the code and get rid of the getline, perhaps something like this:
7
8
9
10
        while (iss >> val)
	{   
            row.push_back (val);	// Add value to row
        }

Last edited on
Topic archived. No new replies allowed.