Text file reading: separate string using vector and structure

Hi C++++ forum,

I'm writing a program to read a text file which has the following format

Number of data points 12
Number of places 6
1990-01-01 1 25.002 -999 -999 16.265 6.231 9.680
1990-01-01 2 24.449 -999 -999 16.265 6.231 9.551
1990-01-01 3 24.449 -999 -999 16.265 6.231 9.551
1990-01-01 4 24.550 -999 -999 16.265 6.231 9.551
1990-01-01 5 24.851 -999 -999 16.265 6.130 9.551
1990-01-01 6 25.002 -999 -999 16.099 6.130 9.421
1990-01-01 7 25.306 -999 -999 15.933 6.130 9.421
1990-01-01 8 25.357 -999 -999 15.933 6.130 9.421
1990-01-01 9 25.357 -999 -999 15.933 6.029 9.389
1990-01-01 10 25.306 -999 -999 15.769 6.029 9.260
1990-01-01 11 25.306 -999 -999 15.769 6.029 9.260
1990-01-01 12 25.103 -999 -999 15.605 6.029 9.132

I'd like to separate the date into year/month/day
My code is:

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
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>

using namespace std;

bool read (const string &filename)
{
    ifstream inputdata(filename.c_str());
    if ( !inputdata )
    {
        cerr << "Error: file could not be opened" << endl;
        return false;
    }

    // A structure used to store time
    struct Time
    {
        int year, month, date;
    };

    string dummy_string;    // temporary string to store unnecessary words
    int n, nplace;
    double temp;
    double** datat;

    // read and set n value
    inputdata >> dummy_string >> dummy_string >> dummy_string >> dummy_string >> n;

    // read and set nplace value
    inputdata >> dummy_string  >> dummy_string  >> dummy_string >> nplace;

    // read data
    int temp2;                     // temp to read hour from 1 to 24
    vector<Time> time_vector;      // create a vector to store time
    datat = new double* [ n ];     // allocate space for an array of pointers
    if (datat != NULL )
    {
        for (int i = 0; i < n; ++i )   // for each of those above pointers, allocate amount of space
        {                              // then we have a 2D array datat[i][j]
            datat[i] = new double [ nplace ];
        }
    }

    Time time_input;
    for (int i = 0; i < n; ++i)
    {
        inputdata >> setw(4) >> time_input.year; // read 4 char and store them in time_input.year
        inputdata.ignore(1);                     // ignore 1 char. in this case "-" char
        inputdata >> setw(2) >> time_input.month;
        inputdata.ignore(1);
        inputdata >> setw(2) >> time_input.date;

        // store the reading result in time_vector
        time_vector.push_back(time_input);
        inputdata >> temp2;                     // read through hour

        for (int j = 0; j < nplace; ++j)
        {
            inputdata >> datat[i][j];
        }
    }

    inputdata.close();
    return true;
}

int main()
{
    read(string("Data.txt"));
    return 0;
}


When I run the program, I received this error message:

...\File_reading_vector.cpp||In function 'bool read(const std::string&)':|
...\File_reading_vector.cpp|41|error: template argument for 'template<class _Alloc> class std::allocator' uses local type 'read(const std::string&)::Time'|
...\File_reading_vector.cpp|41|error: trying to instantiate 'template<class _Alloc> class std::allocator'|
...\File_reading_vector.cpp|41|error: template argument 2 is invalid|
...\File_reading_vector.cpp|41|error: invalid type in declaration before ';' token|
...\File_reading_vector.cpp|61|error: request for member 'push_back' in 'time_vector', which is of non-class type 'int'|
||=== Build finished: 5 errors, 0 warnings ===|

Can anyone tell me what went wrong. Thank you very much

Bests,

TC
Your struct Time is local scope. You need to move its definition outside the function.

Thx Galik. It works perfectly now
Topic archived. No new replies allowed.