array from table in .csv

I want to populate an array from a table of numbers in a .csv file. I've figured out how to open the file, but I can't quite figure out how to get C++ to read in each value for the array element, p[i].

I'm a complete noob, so I don't fully grasp how 'get.allocator' type is supposed to work.

I copied this code from the allocator example, and changed for my situation.

1
2
3
4
5
6
7
8
9
10
11
// allocate an array of 'n' elements using vector's allocator:  -- need to fix this line too...
    p=PowerTurbineFlow.get_allocator().allocate(size_t);

  // assign some values to array
  for (i=0; i<PowerTurbineFlow.size(); i++) p[i]=i;  //fix this line so that P[i] takes values from the .csv file.
  cout << "The allocated array contains:";

  for (i=0; i<PowerTurbineFlow.size(); i++) cout << " " << p[i];
  cout << endl;

return 0;


Thanks in advance.
Use a vector and you're fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <vector>

ifstream fin;
int tmp;                      
vector<int> int_vec;   // vector of integers

fin.open("put your file name here'");

if (fin)   // if file was opened correctly
      while (fin) {
             cin >> tmp;
             int_vec.push_back(tmp);
      }                    
else
     cout << "file corrupt or missing!\n;


 
Last edited on
Topic archived. No new replies allowed.