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 FilesXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 01.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)
/* 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>
usingnamespace std;
constdouble 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;
}