how to store ONLY the 8th column of this txt file

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...

The data looks like this...:

http://www.erh.noaa.gov/er/iln/climo/cvgjan13
this explains how to grab a row...not a column....
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.

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

void foo()
{
    using namespace 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.
Last edited on
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...
Last edited on
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
Last edited on
I just tried to run your code, it didn't work...I'm not sure what you did to get your code to run properly, but for me it isn't working...

I tried to skip over the first 18 lines, and that is cutting out data.

Here's what I have...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

int main(){

	vector <string> weather;
	string line;

	ifstream file;
	file.open("cvgjun13.txt");
	vector<string> column823;

	if(file.is_open()){
		//while(file.good()){
		//for(getline(file, line)){
		for (int i = 0; i <= 16; i++){
	        getline(file, line);

	        //cout<< "line inside for: " << line << endl;
	    }
			
			weather.push_back(line);
			for(int i = 0; i < 30; i++){
				getline(file, line);
				cout<<"line outside for: " << line << endl;
			}
			 stringstream ss(line);
        		string n;
        		for (int i = 0; i < 8; i++){ // skip the first 7 data, keeps data written to last
           		 ss >> n;
           		 column823.push_back(n);


           		}
        // do stuff with n, which now contains the 8th data point of the line
    // /}
//} 

 		//}
 		file.close();
	}

	for(int i = 0; i < weather.size(); i++)
		cout<<i+1 <<": " << weather[i] << endl;
	vector<string> column8;
	vector<string> column82;
	
	
	for(int i = 0; i < weather.size(); i++){
		weather[i].find('.');
		size_t pos = weather[i].find('.');
		column8.push_back(weather[i].substr(pos + 1));
		pos = column8[i].find(' ');
		column82.push_back(column8[i].substr(0, pos));

	}

cout<<"Precipitation Data: "<< endl;
cout<<"================================================================================" << endl;
	std::vector <float> converted_data;
	int x;

	for(int i = 18; i < 50; i++){
		cout<<i+1 <<": " << column82[i] << endl;
		istringstream ss(column82[i]);
		//x = atoi(column82[i]);
		ss >> x;
		converted_data.push_back(x);
	}

int count = 0;
for(int i = 3; i <= 31; i++)
	if(converted_data[i-2] <= 25 && converted_data[i-1] <= 25 && converted_data[i] <= 25)
		cout<<"day: "<< i << " -turn the sprinkler on today! count: " << count++ << endl;


for(int i = 0; i < column823.size(); i++){
		cout<<i+1 <<": " << column823[i] << endl;
}

	return 0;
}


I really couldn't figure out exactly what you are meaning with getting the columns.
Last edited on
nvm, I got it...I had to do some alterations...but it works...thanks d00d.

if anyone is curious about it, reply...it is helpful to know
Last edited on
Can't you just do something like

1
2
3
4
5
6
7
8
9
10
std::string line = "";
std::vector<char> eighthColumn;

std::string const filename = "text.txt");
std::ifstream in(filename);

while(std::getline(in, line))
{
    eighthColumn.push_back(line[7]);
}
Last edited on
Topic archived. No new replies allowed.