reading input from text file in XCode IDE

hi all,

I've read through the documentation on how to use filestreams and I feel very certain that I'm doing it right. My computer disagrees. Here's the code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vector< vector<int> >traversalOrder;
	ifstream travOrder("/Users/rcorty/Desktop/brain/4by10trav.txt",  ifstream::in);
	int count2 = 0;
	for (int i = 0; i < 10000; i++)  {
		cout << "reading in for the " << count2 << " time." << endl;
		count2++;
		vector<int> temp;
		for (int i = 0; i<4; i++) {
			int j;
			travOrder >> j;
			cout << j << endl;
			temp.push_back(j);
		}
		cout << "adding to traversalOrder: " << temp[0] << " " << temp[1] << " " << temp[2] << " " << temp[3] << endl;
		traversalOrder.push_back(temp);
	}



What it attempts to do is read in integers from a file and put them in a large vector of size-4 vectors. Here is a sample of the text file it's reading from:

0,0,0,0
1,0,0,0
0,1,0,0
0,0,1,0
0,0,0,1
2,0,0,0
1,1,0,0
1,0,1,0
.
.
.


And here is a sample of the output it generates:

reading in for the 0 time.
0
0
0
0
adding to traversalOrder: 0 0 0 0
reading in for the 1 time.
0
0
0
0
adding to traversalOrder: 0 0 0 0
reading in for the 2 time.
0
0
0
0
adding to traversalOrder: 0 0 0 0
reading in for the 3 time.
0
0
0
0


At first I just had "4by10Traversal.txt" in the constructor of the fstream, but since it wasn't working I added the complete path. I also had a while loop instead of a for loop and it ran while(!travOrder.eof()), but I hard coded in the 10000 with this for loop when I thought that could help. I also deleted the "=0" from the declaration of j, but then for some reason j got 13*2^13, which seemed even worse than 0. Either way it's clear the data from the file is not being read in. Any suggestions or further questions are much appreciated.
Last edited on
problem was the commas in the input file....replaced them with spaces and it ran perfectly, :)
Topic archived. No new replies allowed.