Reading data from a file into a dynamically allocated array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emaghero
    New Member
    • Oct 2006
    • 85

    #1

    Reading data from a file into a dynamically allocated array

    Afternoon all,

    I am trying to read data from a file into an array. The array size is uknown before I open the file and count the number of data points. The function I have in mind is the following

    Code:
    void read_data(char *file,int &ndata,double *x,double *y)
    {
    	//Read the data from the file and store it in memory
    	//This reads the data stored in the two column file and stores it in memory
    
    	ifstream data;
    
    	data.open(file,ios_base::in);
    	
    	if(!data){
    		cerr<<"Could not open the file"<<endl;
    		cerr<<"Calculation will not occur\n";
    	}
    	else{
    		ndata=0; // Read the number of data points from the file
    
    		while(data.ignore(1280,'\n'))
    			++ndata;
    		data.clear();
    		data.seekg(0);
    
    		ndata--; // Overcounts the number of data points by one
    
    		cout<<"There are "<<ndata<<" lines of data\n";
    
    		x=new(double [ndata+1]); // Resize the arrays according to the number of data points
    		y=new(double [ndata+1]);
    
    		for(int i=1;i<=ndata;i++){ // Read the data into the arrays
    			data>>x[i];
    			data.ignore(100,',');
    			data>>y[i];
    		}
    
    		data.close();
    	}
    }
    This works fine but when I try to run the function inside main I get an out of bounds access error.

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	//Step One: Read the data in from the file
    	int ndpts=0;
    	
    	double *x=0; // Declare the pointers for the arrays to store the data
    	double *y=0;
    
            // Some file names
    	char *f1="Rib_InP_W_1.0_E_0.2_T_0.3_overlap_versus_offset_straight_bend_1.txt";
    	char *f2="Rib_InP_W_1.0_E_0.2_T_0.3_overlap_versus_offset_straight_bend_10.txt";
    	char *f3="Rib_InP_W_1.0_E_0.2_T_0.3_overlap_versus_offset_straight_bend_21.txt";
    	
    	
    	read_data(f1,ndpts,x,y); // Read the data from the file
    
    	for(int i=1;i<ndpts;i++){
    		cout<<i<<" , "<<x[i]<<" , "<<y[i]<<endl;	//Crash and burn!!
    	}
    
    	delete[] x;
    	delete[] y;
    	
    	system("PAUSE");
    	return 0;
    }
    I think the arrays x and y go out of scope once the call read_data is complete, any ideas on how to keep the data in memory? I know I can do this using vector<double> x instead of double *x, but I don't want to have to re-write another function later on to be compatible with vector.

    Thanks.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Check the value of x and y after line 15 has run in main. You will find they are still 0 because you do not pass back the the allocated pointers. The value returned by new remains local ro read_data.

    This code is very unstructured and you could avoid the whole thing by applying a little structure.

    Instead of 2 arrays of double which are clearly related you should declare a structure that holds 2 doubles and then have an array of those structures.

    However then instead an array of those structures you should have a vector of those structures. The vector will handle the memory management for you.

    Comment

    Working...