I'm having some trouble with file input streams.
I read up on all sorts of functions I could use that I thought might help (get, getline, ignore) but I think I'm coming at this at the wrong angle. This is my function which reads in an N by 2 matrix from a file.
My program errors out at 'ins >> D[r][c]' the second time it goes through the for loop (I'm reading in distance graphs, 10 of them in this case.) The error says 'invalid allocation size.' So I'm wondering what is causing that problem and perhaps any thing I can do to fix it.Code:void readGraph ( ifstream& ins, vector< vector<double> > & D, size_t & N ) { ins >> N; D.resize(N+1); for (size_t r=1; r<=N; r++) { D[r].resize(3); for (size_t c=1; c<=2; c++) { //ins >> ch; //ins.putback(ch); ins >> D[r][c]; } } }
Thanks.
Edit: Forgot to mention that I properly open and close the file in the main program (close it after the for loop, of course).



LinkBack URL
About LinkBacks


