I have files containing floating point data that is tab-delimted. I need to read in these files to fill a 2 dimensional vector. The problem here is that I don't know how many floating point data there are per-line in the file (however the number of entries are consistent within the file). So I wanted to use getline to grab each new line of data and then use sscanf to parse the data, but I'm stuck on how to do this. Here's what I've got written so far.
Code:
// >>>>> (1) Try to open the input file for reading
ifstream fin(input_filename.c_str());
if(!fin) {
cerr << "ERROR: Problem opening file " << input_filename << " for reading." << endl;
fin.close();
exit(1);
}
// >>>>> (2) Read in the first line and determine how many PMC data entries
// >>>>> are in the file
char ch;
int pmc_entries = 0;
do {
fin.get(ch);
if (fin.bad()) {
cerr << "ERROR: Problem reading file " << input_filename << endl;
fin.close();
exit(1);
}
if (ch == '\t' || ch == '\n') {
pmc_entries++;
}
} while (ch != '\n');
cout << "File has " << pmc_entries << " pmc entries" << endl;
vector<vector<float> > pmc_data(pmc_entries);
// >>>>> (3) Return to the beginning of the file and read in each line of
// >>>>> data into the pmc_data vector <<<<<
int i = 0;
int v = 0; // A << counter for the pmc_data vector element to fill
char line[1000];
fin.seekg(0); // Go back to the beginning of the file
while (fin.good()) {
fin.getline(line, 1000);
for (i = 0; i < pmc_entries; i++) {
sscanf(line,"%f\t",&pmc_data[v][i]);
}
}
The problem is with the sscanf line inside the inner for loop. It will continually read the same floating point number (the first one in the file) every loop. It would be simple enough for me to construct a string that consisted of multiple "%f\t" for the exact number of entries, but how can I specify where each of those values go (in the pmc_data[][] vector)? Could I use the elipse (...) operator to somehow fill the first dimension of that vector?
Thanks for any ideas you may have