I would like to know the most elegant way to store only the 8th line of this this text file.
I am trying to make a program that will determine when to turn on a sprinkler system...it will look at the data for the past two days and determine if it should turn on the sprinklers...
In your message you said how to store "the 8th line", so some misunderstand certainly arose.
Files read left to right and then top down. If you want to only get the 8th piece of data in each line, you'd have to skip the first 7 elements in the text file each line.
#include <string>
#include <fstream>
#include <sstream>
void foo()
{
usingnamespace std;
ifstream f_in("cvgjan13.txt");
string line;
while (getline(f_in, line)) // stores each line from f_in into a string
{
stringstream ss(line);
string data;
for (int i = 0; i < 8; i++) // skip the first 7 data, keeps data written to last
ss >> data;
// do stuff with data, which now contains the 8th data point of the line
// such as pushing it to an std::vector for later use.
//// Note that I use string instead of double to store the value because
//// it's possible that this value will be a "T"
}
}
Hope I didn't mess up anywhere ;p If anyone else thinks they have a better way feel free to correct me.
I am trying to get the column of data that is in column 7...this is not exactly what I was looking for. I've tried to do several things to your example, but it isn't extracting out the 7th column of data...which is what I'm trying to do....
your example gives me the row of data, not the column...
If you want the 7th column then make it i < 7 instead of i < 8. You said you wanted the 8th.
The 7th column is all zeroes though.. so that doesn't sound useful.
Also you'll have to first skip the first 18 lines to get to the actual data you probably want, I didn't add that into my code.
You'd have to do something like
1 2 3
string line;
for (int i = 0; i < 18; i++)
getline(f_in, line);
My example should be giving the column, so I'm not sure about that.
You'd also need a terminating condition to stop before you read the ======= (50th) line.
I get the following as the 8th column after applying the fixes.
0.01
0.00
0.00
0.00
0.00
T
0.00
0.00
0.08
0.12
0.61
0.04
1.11
0.00
0.01
T
0.00
0.00
0.00
0.00
T
T
T
0.00
0.03
0.00
0.21
0.32
0.00
0.78
0.07