split line and store

Hi everybody :)

I have a txt file with some columns, and I woul like take, for each line only the 7th and 8th values. This values I would like store in a vector or list, I dont know which is better, because I will use these values for plot with ROOT.
So, I have produce a briefly macro that read each line of the file, but i don't know how split all lines and store the values...

#include <iostream>
#include <fstream>

using namespace std;

int main() {
fstream f("bot1");
string s;

while(f.good())
{
getline(f, s);

}
f.close();
return 0;
}




Thank you a lot in advance ;)
Last edited on
An example of the contents of the text file would help.

Meanwhile, this
1
2
3
4
5
while(f.good())
{
getline(f, s);

}
would be simpler and safer like this:
1
2
3
4
while(getline(f, s))
{

}


So, I have produce a briefly macro that ...

I would not use C++ for that. A simple shell script probably does the same. Perhaps with cut or gawk.
http://linux.die.net/man/1/cut
http://linux.die.net/man/1/gawk
Last edited on
Thank you for the answers ;)

An example of the txt file is:

9.2011932 0.00460682814204 9.19946 9.20274 0.00451332920025 0.00467409196828 0.0017332 6.21330995958e-05
9.39669913333 0.00835110132473 9.39174 9.39989 0.00452188041131 0.00853752912756 0.00239086667 0.00382922091342
9.55462930851 0.0117013043781 9.54971 9.56096 0.0114774354918 0.0119186779828 0.00457930851 0.000207112151391
9.59831587786 0.0129239479114 9.59642 9.60044 0.0123479487782 0.0130132329991 0.00212412214 0.000575999133154
9.80157703125 0.0180396615858 9.79974 9.80551 0.0178361160399 0.0182363127178 0.00183703125 1.68351970057e-06
9.90074087719 0.0209201028096 9.89322 9.90676 0.0205776343075 0.0210496611583 0.00601912281 3.29146615785e-05



I have some lines with 8 columns, separate from space, and I have take in a list or vector only the 7th and 8th elements for each line, and I have put them in 2 different list or vector.
I have to use C++ because than the data in list or vector I have to plot with ROOT, so it is necessary use C++.

Thank you a lot!!! :)
Last edited on
Thanks for the file data example.

Here's an outline of reading from the file. A stringstream is used to parse the contents of the line:
1
2
3
4
5
6
7
8
9
10
11
12
    std::ifstream fin("bot1");
    std::string line;

    while (getline(fin, line))
    {
        std::istringstream ss(line);
        std::string s1, s2, s3, s4, s5, s6;
        double d7, d8;
        ss >> s1 >> s2 >> s3 >> s4 >> s5 >> s6;
        if (ss >> d7 >> d8)
            // do something with d7 and d8 here
    }


Here I used string as the type of the first six columns, you could use double for those too if you like. My idea was to show that we are not interested in those columns.

At line 11, nothing too complicated, but it depends what you want. Assuming the pair of values are the coordinates of a point, you could use a simple struct to keep the two values together. Define these before the loop.
1
2
3
4
struct Point {
    double x;
    double y;
}; 
Allocate a vector of such Points.
 
std::vector<Point> points;


Then instead of
1
2
3
double d7, d8;

if (ss >> d7 >> d8)
you would have
1
2
3
4
Point p;

if (ss >> p.x >> p.y)
    points.push_back(p);  // store p in vector points 

Last edited on
Topic archived. No new replies allowed.