I have several text files of columns of data. Some have three columns, some have four. All of the columns have headers on them. I need to be able to break each test file into an array based on the columns. I need to be able to access the data (doubles) in the columns. I will be using a regular expression to figure out which column has the data I need, based on the header in the array.
fstream file("...");
string temp;
getline(file, temp);//read the first line with headers
stringstream ss(headers);
int count = 0, index = -1;
for(; ss >> temp; count++)
if(/*temp is the string you wanted*/) index = count;
//now count shows how many columns you have
//and index shows which one you want
//index = -1 indicates failure
double val;
for(int i = 0; i < index; i++) file >> val;//skip the first several columns
vector<double> values;//you can use an array if you know how long the lists are.
//You'll want to make the following loop into an for loop in that case.
while(file){//while input hasn't failed (due to end of file or a string instead of a number)
file >> val;
values.push_back(val);//read and push a value you need into the vector.
//if you want an array, the two lines above would turn into
//file >> values[counter]; where counter is the variable of the for loop I mentioned.
for(int i = 0; i < count-1; i++) file >> val;//skip the remaining entries of this line and some of the other.
}
Something like that. I'm assuming the file doesn't have empty entries. That would make it more complicated..
while (!datFile.eof())
{
string temp;
getline (datFile, temp);
stringstream ss(headers);
int count = 0, index = -1;
for (; ss >> temp; count++)
{
if (temp...)
{
index = count;
}
}
double val;
for (int i = 0; i < index; i++)
{
datFile >> val
}
vector<double> values;
while (datFile)
{
datFile >> val;
values.push_back(val);
}
stringstream ss(headers) doesn't work - what is "headers"?
I want the first column and the column with the data I want, so how do I modify lines 19-22 for this purpose?
I obviously need something more on line 12 where I have temp... - I need to do a search (using regular expressions?) to see if that header contains "eu" or "EU" - Do you know how to do this, or should I post again in the forum?
I must apologize for this post - I'm used to having the Qt infrastructure to help me out, but I don't have it on this computer.
string name;
fstream inFile;
fstream datFile;
inFile.open ("input.txt");
if (!inFile)
{
cerr << "File open failure\n";
exit(EXIT_FAILURE);
}
while (!inFile.eof())
{
getline (inFile, name);
datFile.open(name);
if (!datFile)
{
cerr << "File open failure\n";
exit(EXIT_FAILURE);
}
while (!datFile.eof())
{
string temp;
getline (datFile, temp);
stringstream ss(headers);
int count = 0, index = -1;
for (; ss >> temp; count++)
{
if (temp...)
{
index = count;
}
}
double val;
for (int i = 0; i < index; i++)
{
datFile >> val;
}
vector<double> values;
while (datFile)
{
datFile >> val;
values.push_back(val);
}
}
See above notes for questions about "temp..." and "datFile >> val".
I want to have multiple vectors (one for each file). The above code will loop over all the "datFile" names listed in "inFile.txt", and create a vector for each one.
You have way too many if (!inFile) and similar checks in your code. It might be a good idea to check once that the file was opened successfully and you really need the one I had in my code, but there is no point in having all of the rest.
Edit: didn't notice you had two files.. Line 25 is still not needed though.
About if(/*temp is the string you wanted*/), I assumed you had the regex part figured out. Well, you could do as you said and get yourself a regex library like Boost.Regex. Though I think with C++11 there is a standard one..
Or you could simply use http://cplusplus.com/reference/string/string/find/ which would be sufficient for your current simple needs.
About the whole thing, if you don't need just one line, I suggest you read all of the data and then pick out what you want. That way it will be less of a mess. You'll need a vector of strings for headers and a vector of vectors of doubles for the columns. Ask if you can't figure out how that would work.
I'm going to be going through the data, doing some interpolation, and eventually generating +/- 3-sigmas for all the data points. I'll play with C++ vectors and see what I come up with. If I can't solve it, I'll post back.