#include #include using namespace std; typedef unsigned char byte; typedef signed char sbyte; struct observation { short int year; byte month, day; short int mint, avgt, maxt; sbyte snow, rain; byte wind; }; int main() { if (sizeof(observation) != 14) { cerr << "Look out! sizeof(observation) is " << sizeof(observation) << ", not 14\n"; exit(1); } cout << "OK so far\n"; ifstream dfin("data.txt"); if (dfin.fail()) { cerr << "Can't open data.txt\n"; exit(1); } fstream bfout("data.dat", ios::out | ios::binary | ios::trunc); if (bfout.fail()) { cerr << "Can't create data.dat\n"; exit(1); } int num = 0; while (true) { observation ob; int year, month, day, mint, avgt, maxt, snow, rain, wind; dfin >> year >> month >> day >> mint >> avgt >> maxt >> snow >> rain >> wind; if (dfin.fail()) break; ob.year = year; ob.month = month; ob.day = day; ob.mint = mint; ob.avgt = avgt; ob.maxt = maxt; ob.snow = snow; ob.rain = rain; ob.wind = wind; bfout.write((char *) & ob, sizeof(ob)); num += 1; } cout << num << " records transferred\n"; bfout.close(); dfin.close(); }