Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- struct TimeVal
- {
- int h;
- int m;
- int s;
- };
- void TxtInBin(char* input, char* output)
- {
- fstream in(input, ios::in);
- fstream out(output, ios::out, ios::binary);
- if (in.fail() || out.fail())
- {
- cout << "couldn't open a file" << endl;
- in.close();
- out.close();
- exit(-1);
- }
- else
- {
- int n;
- double threshold;
- in >> n >> threshold;
- out.write((char*)&n, sizeof(int));
- out.write((char*)&threshold, sizeof(double));
- TimeVal* time = new TimeVal[n];
- double speed;
- for (int i = 0; i < n; i++)
- {
- in >> time[i].h;
- in.ignore();
- in >> time[i].m;
- in.ignore();
- in >> time[i].s;
- in >> speed;
- out.write((char*)&time[i], sizeof(TimeVal));
- out.write((char*)&speed, sizeof(double));
- }
- }
- in.close();
- out.close();
- }
- void Read(char* input, int& n, TimeVal*& time, double*& arrWithData, double& threshold, int& count)
- {
- fstream in(input, ios::in, ios::binary);
- if (in.fail())
- {
- cout << "couldn't open a file" << endl;
- in.close();
- exit(-1);
- }
- else
- {
- in.read((char*)&n, sizeof(int));
- in.read((char*)&threshold, sizeof(double));
- arrWithData = new double[n];
- time = new TimeVal[n];
- count = 0;
- for (int i = 0; i < n; i++)
- {
- in.read((char*)&time[i], sizeof(TimeVal));
- in.read((char*)&arrWithData[i], sizeof(double));
- if (arrWithData[i] > threshold)
- count++;
- }
- }
- in.close();
- }
- void Res(double& threshold, int& n, TimeVal*& time, double*& arrWithData, int& count, TimeVal*& timeRes, double*& arrWithRes)
- {
- arrWithRes = new double[count];
- timeRes = new TimeVal[count];
- count = 0;
- for (int i = 0; i < n; i++)
- {
- if (arrWithData[i] > threshold)
- {
- timeRes[count] = time[i];
- arrWithRes[count] = arrWithData[i];
- count++;
- }
- }
- }
- void Write(char* output, int& count, TimeVal*& time, double*& arrWithRes)
- {
- fstream out(output, ios::out, ios::binary);
- if (out.fail())
- {
- cout << "couldn't open a file" << endl;
- out.close();
- exit(-1);
- }
- else
- {
- out.write((char*)&count, sizeof(int));
- for (int i = 0; i < count; i++)
- {
- out.write((char*)&time[i], sizeof(TimeVal));
- out.write((char*)&arrWithRes[i], sizeof(double));
- }
- }
- out.close();
- }
- void BinInTxt(char* input, char* output)
- {
- fstream in(input, ios::in, ios::binary);
- fstream out(output, ios::out);
- if (in.fail() || out.fail())
- {
- cout << "couldn't open a file" << endl;
- out.close();
- in.close();
- exit(-1);
- }
- else
- {
- int count;
- in.read((char*)&count, sizeof(int));
- out << count << endl;
- double* arrWithRes = new double[count];
- TimeVal* time = new TimeVal[count];
- for (int i = 0; i < count; i++)
- {
- in.read((char*)&time[i], sizeof(TimeVal));
- in.read((char*)&arrWithRes[i], sizeof(double));
- }
- for (int i = 0; i < count; i++)
- {
- out.width(2);
- out.fill('0');
- out << time[i].h << ":";
- out.width(2);
- out.fill('0');
- out << time[i].m << ":";
- out.width(2);
- out.fill('0');
- out << time[i].s << " ";
- out << fixed << setprecision(1) << arrWithRes[i] << endl;
- }
- delete[] arrWithRes;
- }
- out.close();
- in.close();
- }
- int main()
- {
- int n;
- double threshold;
- int count;
- TimeVal* time;
- double* arrWithData = {};
- TimeVal* timeRes;
- double* arrWithRes = {};
- char input[] = "input.txt";
- char output[] = "output.txt";
- char inputb[] = "input.bin";
- char outputb[] = "output.bin";
- TxtInBin(input, inputb);
- Read(inputb, n, time, arrWithData, threshold, count);
- Res(threshold, n, time, arrWithData, count, timeRes, arrWithRes);
- Write(outputb, count, timeRes, arrWithRes);
- BinInTxt(outputb, output);
- delete[] time;
- delete[] arrWithData;
- delete[] arrWithRes;
- delete[] timeRes;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement