473,325 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Reading data from a file into a dynamically allocated array

emaghero
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

Expand|Select|Wrap|Line Numbers
  1. void read_data(char *file,int &ndata,double *x,double *y)
  2. {
  3.     //Read the data from the file and store it in memory
  4.     //This reads the data stored in the two column file and stores it in memory
  5.  
  6.     ifstream data;
  7.  
  8.     data.open(file,ios_base::in);
  9.  
  10.     if(!data){
  11.         cerr<<"Could not open the file"<<endl;
  12.         cerr<<"Calculation will not occur\n";
  13.     }
  14.     else{
  15.         ndata=0; // Read the number of data points from the file
  16.  
  17.         while(data.ignore(1280,'\n'))
  18.             ++ndata;
  19.         data.clear();
  20.         data.seekg(0);
  21.  
  22.         ndata--; // Overcounts the number of data points by one
  23.  
  24.         cout<<"There are "<<ndata<<" lines of data\n";
  25.  
  26.         x=new(double [ndata+1]); // Resize the arrays according to the number of data points
  27.         y=new(double [ndata+1]);
  28.  
  29.         for(int i=1;i<=ndata;i++){ // Read the data into the arrays
  30.             data>>x[i];
  31.             data.ignore(100,',');
  32.             data>>y[i];
  33.         }
  34.  
  35.         data.close();
  36.     }
  37. }
  38.  
This works fine but when I try to run the function inside main I get an out of bounds access error.

Expand|Select|Wrap|Line Numbers
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3.     //Step One: Read the data in from the file
  4.     int ndpts=0;
  5.  
  6.     double *x=0; // Declare the pointers for the arrays to store the data
  7.     double *y=0;
  8.  
  9.         // Some file names
  10.     char *f1="Rib_InP_W_1.0_E_0.2_T_0.3_overlap_versus_offset_straight_bend_1.txt";
  11.     char *f2="Rib_InP_W_1.0_E_0.2_T_0.3_overlap_versus_offset_straight_bend_10.txt";
  12.     char *f3="Rib_InP_W_1.0_E_0.2_T_0.3_overlap_versus_offset_straight_bend_21.txt";
  13.  
  14.  
  15.     read_data(f1,ndpts,x,y); // Read the data from the file
  16.  
  17.     for(int i=1;i<ndpts;i++){
  18.         cout<<i<<" , "<<x[i]<<" , "<<y[i]<<endl;    //Crash and burn!!
  19.     }
  20.  
  21.     delete[] x;
  22.     delete[] y;
  23.  
  24.     system("PAUSE");
  25.     return 0;
  26. }
  27.  
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.
Apr 28 '10 #1
1 5111
Banfa
9,065 Expert Mod 8TB
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.
Apr 28 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
3
by: yogi | last post by:
Hi guys, I'm trying to write a program that will read in a series of files and create a 3D array from the files read in for converting 2D images to 3D objects. The values read in will be...
3
by: juleigha27 | last post by:
I can't figure out what is going on with this code. I don't know why it won't let me increment i without crashing. My data file looks like this: 1343 5.66666 DOG 2334 3.44444 FROG ...
6
by: Giff | last post by:
Hi I have this problem that I can't solve, I know it shouldn't be hard but I'm not a good coder and I'm going crazy tonight... I have a txt file that goes like this: string1 string2...
7
by: John Smith | last post by:
I want to read data from a file and assign it to a dynamically allocated array. I don't know the number of data in advance. My approach has been to read the file twice, the first time to determine...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
13
by: rsk | last post by:
Hi Friends, My requirement is as follows; A file is consisting of data in hexadecimal format(i.e a 32 bit data for example like "0xdeadbeef"). I have to read each of such data into my 'c'...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
7
by: pereges | last post by:
I've to store an array of structures: typedef struct { double origin; double e_field_at_origin_real, e_field_at_origin_imag; double direction; double pathlength; int depth; }ray;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.