//: C04:DataLogger.h // Datalogger record layout. #ifndef DATALOG_H #define DATALOG_H #include <ctime> #include <iosfwd> #include <string> using std::ostream; struct Coord { int deg, min, sec; Coord(int d = 0, int m = 0, int s = 0) : deg(d), min(m), sec(s) {} std::string toString() const; }; ostream& operator<<(ostream&, const Coord&); class DataPoint { std::time_t timestamp; // Time & day Coord latitude, longitude; double depth, temperature; public: DataPoint(std::time_t ts, const Coord& lat, const Coord& lon, double dep, double temp) : timestamp(ts), latitude(lat), longitude(lon), depth(dep), temperature(temp) {} DataPoint() : timestamp(0), depth(0), temperature(0) {} friend ostream& operator<<(ostream&, const DataPoint&); }; #endif // DATALOG_H ///:~
Listado 5.23. C04/DataLogger.h
//: C04:DataLogger.cpp {O} // Datapoint implementations. #include "DataLogger.h" #include <iomanip> #include <iostream> #include <sstream> #include <string> using namespace std; ostream& operator<<(ostream& os, const Coord& c) { return os << c.deg << '*' << c.min << '\'' << c.sec << '"'; } string Coord::toString() const { ostringstream os; os << *this; return os.str(); } ostream& operator<<(ostream& os, const DataPoint& d) { os.setf(ios::fixed, ios::floatfield); char fillc = os.fill('0'); // Pad on left with '0' tm* tdata = localtime(&d.timestamp); os << setw(2) << tdata->tm_mon + 1 << '\\' << setw(2) << tdata->tm_mday << '\\' << setw(2) << tdata->tm_year+1900 << ' ' << setw(2) << tdata->tm_hour << ':' << setw(2) << tdata->tm_min << ':' << setw(2) << tdata->tm_sec; os.fill(' '); // Pad on left with ' ' streamsize prec = os.precision(4); os << " Lat:" << setw(9) << d.latitude.toString() << ", Long:" << setw(9) << d.longitude.toString() << ", depth:" << setw(9) << d.depth << ", temp:" << setw(9) << d.temperature; os.fill(fillc); os.precision(prec); return os; } ///:~
Listado 5.24. C04/DataLogger.cpp
//: C04:Datagen.cpp // Test data generator. //{L} DataLogger #include <cstdlib> #include <ctime> #include <cstring> #include <fstream> #include "DataLogger.h" #include "../require.h" using namespace std; int main() { time_t timer; srand(time(&timer)); // Seed the random number generator ofstream data("data.txt"); assure(data, "data.txt"); ofstream bindata("data.bin", ios::binary); assure(bindata, "data.bin"); for(int i = 0; i < 100; i++, timer += 55) { // Zero to 199 meters: double newdepth = rand() % 200; double fraction = rand() % 100 + 1; newdepth += 1.0 / fraction; double newtemp = 150 + rand() % 200; // Kelvin fraction = rand() % 100 + 1; newtemp += 1.0 / fraction; const DataPoint d(timer, Coord(45,20,31), Coord(22,34,18), newdepth, newtemp); data << d << endl; bindata.write(reinterpret_cast<const char*>(&d), sizeof(d)); } } ///:~
Listado 5.25. C04/Datagen.cpp
//: C04:Datascan.cpp //{L} DataLogger #include <fstream> #include <iostream> #include "DataLogger.h" #include "../require.h" using namespace std; int main() { ifstream bindata("data.bin", ios::binary); assure(bindata, "data.bin"); DataPoint d; while(bindata.read(reinterpret_cast<char*>(&d), sizeof d)) cout << d << endl; } ///:~
Listado 5.26. C04/Datascan.cpp