I wrote this code to read from a CSV text file (containing data such as 12,3,568,48,3,8 with no more than 3 digits to each number). It stores the numbers as char arrays in the vector values
. However, this does seem like a clumsy way of doing this with resizing the vector and copying the chars. Is there a neater/more succinct way of doing this?
#include <iostream>
#include <fstream>
#include <assert.h>
#include <vector>
int main() {
using namespace std;
ifstream in;
in.open("Data.txt");
assert(in.is_open());
vector<char*> values;
const int MAXSIZE = 4;
char thisVal[MAXSIZE];
while(in.getline(thisVal,MAXSIZE,',')) {
values.resize(values.size() + 1);
values.back() = new char[MAXSIZE];
strcpy(values.back(), thisVal);
}
in.close();
for (char* cp: values) cout<<*cp<<endl;
return 0;
}