How do I read certain parts on certain lines in a text file?

So basically I'm trying to read everything from day 2 on 1.0m and then the same with .5m and .25m and add them up and provide an average. Same with day 3. What I can't figure out is, how do I get those specific numbers in the text file all the way to the end of file assuming that we don't know when the file ends? I take it I need to read the first four lines and not process them but when I get to the lines that I need to process I can't' figure out how to get specifically the temps of only day 2 on 1.0m so that I can add them up using fstream.

This is the file I'm talking about
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
Mars temperature data
 ~ 24 hrs over two Solar days
Sol Local T  Dec Sol Temperature (Celsius)
                        1.0m    0.5  0.25m
2  11:59:08  2.486035  -25.4  -20.8  -17.4  
2  12:28:08  2.505635  -23.4  -18.8  -14.9  
2  12:57:08  2.525235  -22.4  -17.8  -14.1  
2  13:26:09  2.544851  -21.0  -16.5  -12.6  
2  13:55:10  2.564458  -19.7  -15.3  -11.5  
2  14:24:10  2.584058  -15.9  -13.0   -9.6  
2  14:53:11  2.603669  -15.3  -13.6  -10.2  
2  15:22:11  2.623269  -15.3  -12.7   -9.7  
2  15:51:12  2.642880  -18.9  -18.2  -15.1  
2  16:20:13  2.662490  -21.1  -20.9  -18.3  
2  16:49:13  2.682092  -23.2  -23.2  -21.3  
2  17:18:14  2.701703  -26.9  -26.9  -25.9  
2  17:47:15  2.721314  -30.8  -31.1  -30.2  
2  18:45:17  2.760537  -43.8  -43.9  -44.4  
2  19:14:18  2.780148  -45.7  -46.1  -46.9  
2  19:43:19  2.799760  -49.1  -49.4  -50.2  
2  20:12:20  2.819371  -51.5  -51.8  -52.7  
2  20:41:21  2.838982  -55.5  -55.8  -56.4  
2  21:10:22  2.858594  -57.5  -57.7  -58.4  
2  21:39:23  2.878205  -61.0  -61.6  -63.0  
2  22:08:24  2.897816  -61.1  -61.2  -62.7  
2  22:37:25  2.917428  -64.5  -64.9  -65.7  
2  23:06:26  2.937039  -64.9  -65.5  -66.9  
2  24:04:28  2.976262  -67.8  -68.5  -70.2  
3  00:51:55  3.035084  -69.3  -70.0  -71.1  
3  01:49:57  3.074307  -70.4  -70.7  -73.4  
3  02:18:58  3.093918  -71.3  -71.5  -73.2  
3  02:47:59  3.113530  -71.9  -72.3  -73.9  
3  04:44:02  3.191964  -75.0  -75.4  -77.7  
3  05:13:03  3.211575  -76.0  -76.4  -77.8  
3  05:42:04  3.231186  -74.7  -74.9  -75.9  
3  06:11:04  3.250786  -71.6  -71.7  -72.0  
3  06:40:05  3.270398  -67.2  -66.8  -65.0  
3  07:09:05  3.289998  -63.7  -63.0  -61.9  
3  07:38:06  3.309609  -59.5  -58.6  -55.4  
3  08:07:06  3.329209  -53.0  -51.9  -46.7  
3  08:36:07  3.348820  -47.7  -46.9  -44.6  
3  09:05:07  3.368421  -44.3  -42.3  -40.0  
3  09:34:08  3.388032  -42.0  -39.9  -35.4  
3  10:03:08  3.407632  -36.0  -34.1  -30.4  
3  10:32:09  3.427243  -32.6  -28.8  -25.6  
3  11:01:09  3.446843  -29.8  -26.0  -22.4  
3  11:29:54  3.466274  -27.0  -23.5  -20.4  
3  12:27:55  3.505486  -22.3  -19.2  -15.7


Any help is extremely appreciated!
Last edited on
Anyone? I'm getting desperate and google hasn't been helping me for the past 3 hours =/.
Try looking at it backwards, how would you print that file?
I honestly don't know.....I've been working on trying to figure this specific thing out for so long that I'm brainfarting everything I read and it's going way over my head because I'm getting too frustrated over something that is probably extremely simple and I have to wake up in a couple of hours to turn this in. Oh well.
The logic for extracting the data of interest could be something like this:

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
#include <iostream>
#include <sstream>
#include <vector>

struct temp_info // holds information in one line
{
    int day ;
    std::string local_time ;
    double dec_sol ;

    enum datum { ONE_M, POINT_FIVE_M, POINT_TWO_FIVE_M } ;
    double values[3] ;
};

temp_info to_temp_info( const std::string& line ) // retrieve  info from the line
{
     std::istringstream stm(line) ;
     temp_info ti ;
     if( stm >> ti.day >> ti.local_time >> ti.dec_sol )
     {
         for( double& v : ti.values ) stm >> v ;
         if(stm) return ti ;
     }
     return temp_info{} ; // value-initialised (day==0)
}

std::istream& skip_headers( std::istream& stm, int nlines = 4 )
{ while( nlines-- ) stm.ignore( 1000000, '\n' ) ; return stm ; }

// return selected day-temperature pairs
std::vector< std::pair<int,double> > read_filtered( std::istream& stm, int day, temp_info::datum col )
{
    skip_headers(stm) ;
    std::vector< std::pair<int,double> > result ;

    std::string line ;
    while( std::getline( stm, line ) )
    {
        auto ti = to_temp_info(line) ;
        if( ti.day == day ) // if this line has information about the day of interest
            result.emplace_back( ti.day, ti.values[col] ) ; // pick up the appropriate col. and add to result
    }

    return result ;
}

int main()
{
    std::istringstream file(
        R"(Mars temperature data
         ~ 24 hrs over two Solar days
        Sol Local T  Dec Sol Temperature (Celsius)
                                1.0m    0.5  0.25m
        2  11:59:08  2.486035  -25.4  -20.8  -17.4
        2  12:28:08  2.505635  -23.4  -18.8  -14.9
        3  04:44:02  3.191964  -75.0  -75.4  -77.7
        3  05:13:03  3.211575  -76.0  -76.4  -77.8
        3  05:42:04  3.231186  -74.7  -74.9  -75.9
        3  06:11:04  3.250786  -71.6  -71.7  -72.0
        3  02:47:59  3.113530  -71.9  -72.3  -73.9
        3  10:32:09  3.427243  -32.6  -28.8  -25.6
        3  11:01:09  3.446843  -29.8  -26.0  -22.4
        2  15:22:11  2.623269  -15.3  -12.7   -9.7
        2  15:51:12  2.642880  -18.9  -18.2  -15.1
        1  16:20:13  2.662490  -21.1  -20.9  -18.3
        5  19:14:18  2.780148  -45.7  -46.1  -46.9
        1  19:43:19  2.799760  -49.1  -49.4  -50.2
        2  20:12:20  2.819371  -51.5  -51.8  -52.7
        2  20:41:21  2.838982  -55.5  -55.8  -56.4
        4  23:06:26  2.937039  -64.9  -65.5  -66.9
        2  24:04:28  2.976262  -67.8  -68.5  -70.2
        3  00:51:55  3.035084  -69.3  -70.0  -71.1
        3  01:49:57  3.074307  -70.4  -70.7  -73.4
        4  02:18:58  3.093918  -71.3  -71.5  -73.2
        3  11:29:54  3.466274  -27.0  -23.5  -20.4
        3  12:27:55  3.505486  -22.3  -19.2  -15.7
        )"
    );

    for( auto pair : read_filtered( file, 2, temp_info::POINT_FIVE_M ) ) // day 2, temperature @0.5m
        std::cout << "measurement on day " << pair.first << "   temperature @0.5m is " << pair.second << '\n' ;
}

http://coliru.stacked-crooked.com/a/0c6e7bd722c16cc0

You would need to implement some logic like this in your own code.
Topic archived. No new replies allowed.