Thread: Need some help with file input streams

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    Need some help with file input streams

    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.

    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];
    
    		}
    	}	
    }
    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.

    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).
    Last edited by ProFiction; 10-22-2005 at 11:14 PM.

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    A more detailed version of the error I provided in my post is 'Invalid allocation size: 4294967288 bytes.

    I thought this meant that it was making my N value too large and when it goes to resize D it errors out. I checked to see if my N value was right and it appeared to be, but I could be wrong on that.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Your code works fine for me:
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    
    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];
    
    		}
    	}	
    }
    
    int main()
    {
    	ifstream input("C:\\TestData\\data.txt");
    	if(!input)
    	{
    		cout<<"Failed to open file."<<endl;
    		return 1;
    	}
    	
    	vector< vector<double> > matrix;
    	size_t N;
    	
    	readGraph(input, matrix, N);
    
    	for(int i=1; i < N+1; i++)
    	{
    		for(int j=1; j<3; j++)
    		{
    			cout<<matrix[i][j]<<" ";
    		}
    		cout<<endl;
    	}
    
    
    	return 0;
    }
    C:\TestData\data.txt:
    Code:
    3
    1 2 
    4 5 
    7 8
    However, when reading from a file, you should use the read statements as your loop conditionals, so that if a stream error occurs things will end well. Otherwise, your program could get stuck in an infinite loop.
    Last edited by 7stud; 10-23-2005 at 02:40 AM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Oh, I should have been more specific.

    Here's a portion of my main program:

    Code:
    string filename;
    	cout << "Input file: ";
    	cin >> filename;
    	cout << endl;
    
    
    	ifstream ins;
    	ins.open(filename.c_str());
    	assert (ins);
    
    	ins >> T;
    	cout << T << endl << endl;
    	//loops the number of times that T calls
    	for(size_t b = 0; b < T; b++)
    	{
    		dis = 0;
    		N = 0;
    		readGraph (ins, W, N);//calls read graph
                                    //etc
                    }
    And the input file looks like this:
    Code:
    10
    
    2
    1.4 0.4
    1.0 0.3
    
    1
    3.4 5.0
    
    10
    4.2 1.6
    0.3 4.6
    6.2 0.0
    8.9 6.8
    3.9 6.7
    6.0 3.3
    9.7 7.4
    1.5 0.7
    6.5 8.9
    2.1 3.0
    
    1
    6.6 7.3
    ................... etc
    As you can see, it reads in the 10 as the number of times to run through the loop that calls readGraph. And the second time through readGraph there is a problem. Sorry for not being so clear, but thank you for the reply. If you could provide any more help, that would be terrific.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Alright, just did a lot of testing and the problem isn't in readGraph. It's in another function which is called in the same for loop.

    Code:
    void distance ( size_t N,
    			   const vector< vector<double> > & W,
    			   vector< vector<double> > & P)
    {
    	P.resize(N+1);//resizes the distance matrix
    	for (size_t r=1; r <= N; r++)
    	{
    		P[r].resize(N+1);
    		for(size_t c=1; c<=N; c++)
    		{
    			P[r][c]=0;
    		}
    	}
    
    	//finds the distance from each point
    	for(size_t i=1; i<= N; i++)
    	{
    		for(size_t j=1; j<=N; j++)
    		{
    			if (j==i)
    				P[i][j]=0;
    			else if (j>i) 
    			{
    				P[i][j]=sqrt(((W[i][1]-W[j][1])*(W[i][1]-W[j][1]))+(W[i][2]-W[j][2])*(W[i][2]-W[j][2]));
    				P[j][i]=P[i][j];
    			}
    		}
    	}
    The error occurs the second time the function is called (N = 1), somewhere near distance's for loop indexed by j from 1 to N.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Holy christ I'm an idiot.

    Forget this entire thread happened.

    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. File I/O with 2 input and 1 output file
    By Sandy in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 12:06 PM