file to 2d array

Apr 10, 2014 at 2:36pm
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
Apr 10, 2014 at 2:42pm
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?
Apr 10, 2014 at 2:43pm
rows and columns can variate. this is just one of examples.
Apr 10, 2014 at 3:01pm
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 Apr 10, 2014 at 3:02pm
Apr 10, 2014 at 3:23pm
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)?
Apr 10, 2014 at 4:01pm
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.
Apr 10, 2014 at 10:38pm
I didn't get how you count and delimited columns, could you please show me where in parse_data_row() can I count columns?
Apr 11, 2014 at 1:21pm
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.
Apr 11, 2014 at 8:07pm
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
}
Apr 11, 2014 at 8:29pm
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.


Apr 11, 2014 at 8:35pm
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 Apr 11, 2014 at 9:04pm
Topic archived. No new replies allowed.