Extract data from file with header

Hi all,

I have been trying to read a series of data from various .dat files into an array of ints (only one file per execution of the code). The data comes after a header section in the file (which is always 13 lines long) and I was hoping somebody could explain to me how to seek to the start of the data so that I can use a for loop to read the data into the array. So you have an idea the data file looks like this:


Version: 3
XXXXXXXXXX: 1
Sample ID: XXXX XXXXX
Data File: C:Program FilesXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX01.dat
XXXXX: C:Program FilesXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
User Name: XXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: 28/10/2010 09:04:03
XXXXXXXX XXXX: XXXXXXXX XX
Total Data Points: 3001 Pts.
X XXXXX XXXXX: XXXXXXX
X XXXXX XXXXX: XXXXXXX
X XXXX XXXXXXXX: XXXXXXXX
X XXXX XXXXXXXX: XXXXX
4
2
3
0
1
4
7
6
6
9
10
7
3
4
6
4
1
1
6
6
4
3
4
4
4
8
8


The "X"s are not of fixed length but it is always 13 lines and the number of data points can be tens of thousands. Thanks in advance for any help!

(I am ussing Windows 7 64-bit and Code::Blocks to compile)
I'll add the code that I have so far in here:

(Obviously it's not finished yet. The part I would really appreciate some help with is the bit labelled as "SEARCH FOR START OF DATA". )
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Radioatctive decay correction program for data from radiation detector. This version
   corrects for FLUORINE-18 which has a half-life of 6586 seconds (from XXXXXXX)*/


#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdlib>

using namespace std;

const double halflife = 6586;   // Half-Life of F-18 in seconds

int main()
{

    int r; //"header" array element number for: 1) sampling rate (Hz)
    int s; // 2) start of data
    string header[100]; // Array of string to read words from header into
    double data[100]; // Array of doubles to read detector data into


    // OPEN THE DATA FILE --------------------------------------------------------------------------------------------------

    ifstream datafile;

    datafile.open("C:\data1.txt");

    if(!datafile) cerr << "Could not open the data file" << endl;

    // READ SIZE OF FILE ---------------------------------------------------------------------------------------------------

    // SEARCH FOR THE TIME INTERVAL ----------------------------------------------------------------------------------------

    datafile.seekg(0);

    if(datafile)
    {

        for(int i=0;i<100;i++)
        {
            datafile >> header[i];

            if(header[i] == "Rate:")
            {
                r = i+1;
            }

            if(header[i] == "Multiplier:")
            {
                s = i+2;

            }

        }

        cout << "Sampling Rate is " << header[r] << " Hz." << endl;

        double rate = atof(header[r].c_str());

        double interval = (1/rate); // Calculates the time between data points from the sampling rate.

        cout << "Sampling interval is " << interval << " seconds." << endl;

        cout << "The first data point is '" << header[s] << "' " << endl;

        cout << "The file pointer is at " << datafile.tellg();
    }


    // SEARCH FOR START OF DATA --------------------------------------------------------------------------------------------

    // READ IN DATA FROM FILE ----------------------------------------------------------------------------------------------

    // PERFORM HALF-LIFE CORRECTION ON DATA --------------------------------------------------------------------------------

    /* for(XXXXXX)
        {

        }

    */


    // CREATE OUTPUT FILE --------------------------------------------------------------------------------------------------

    ofstream newdata;

    newdata.open("Decaycorrected.txt");

    newdata << "a, b, c" << endl;

    // SAVE CORRECTED DATA TO OUTPUT FILE ----------------------------------------------------------------------------------

    // CLOSE INPUT AND OUTPUT FILES ----------------------------------------------------------------------------------------

    datafile.close();
    newdata.close();

    return 0;
}
Topic archived. No new replies allowed.