SHARE
TWEET

mlita

Invizer Nov 20th, 2019 (edited) 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. struct TimeVal
  8. {
  9.     int h;
  10.     int m;
  11.     int s;
  12. };
  13.  
  14. void TxtInBin(char* input, char* output)
  15. {
  16.     fstream in(input, ios::in);
  17.     fstream out(output, ios::out, ios::binary);
  18.  
  19.     if (in.fail() || out.fail())
  20.     {
  21.         cout << "couldn't open a file" << endl;
  22.         in.close();
  23.         out.close();
  24.         exit(-1);
  25.     }
  26.     else
  27.     {
  28.         int n;
  29.         double threshold;
  30.  
  31.         in >> n >> threshold;
  32.  
  33.         out.write((char*)&n, sizeof(int));
  34.         out.write((char*)&threshold, sizeof(double));
  35.  
  36.         TimeVal* time = new TimeVal[n];
  37.         double speed;
  38.  
  39.         for (int i = 0; i < n; i++)
  40.         {
  41.             in >> time[i].h;
  42.             in.ignore();
  43.             in >> time[i].m;
  44.             in.ignore();
  45.             in >> time[i].s;
  46.             in >> speed;
  47.             out.write((char*)&time[i], sizeof(TimeVal));
  48.             out.write((char*)&speed, sizeof(double));
  49.         }
  50.     }
  51.  
  52.     in.close();
  53.     out.close();
  54. }
  55.  
  56. void Read(char* input, int& n, TimeVal*& time, double*& arrWithData, double& threshold, int& count)
  57. {
  58.     fstream in(input, ios::in, ios::binary);
  59.  
  60.     if (in.fail())
  61.     {
  62.         cout << "couldn't open a file" << endl;
  63.         in.close();
  64.         exit(-1);
  65.     }
  66.     else
  67.     {
  68.         in.read((char*)&n, sizeof(int));
  69.         in.read((char*)&threshold, sizeof(double));
  70.  
  71.         arrWithData = new double[n];
  72.         time = new TimeVal[n];
  73.  
  74.         count = 0;
  75.         for (int i = 0; i < n; i++)
  76.         {
  77.             in.read((char*)&time[i], sizeof(TimeVal));
  78.             in.read((char*)&arrWithData[i], sizeof(double));
  79.  
  80.             if (arrWithData[i] > threshold)
  81.                 count++;
  82.         }
  83.     }
  84.     in.close();
  85. }
  86.  
  87. void Res(double& threshold, int& n, TimeVal*& time, double*& arrWithData, int& count, TimeVal*& timeRes, double*& arrWithRes)
  88. {
  89.     arrWithRes = new double[count];
  90.     timeRes = new TimeVal[count];
  91.     count = 0;
  92.  
  93.     for (int i = 0; i < n; i++)
  94.     {
  95.         if (arrWithData[i] > threshold)
  96.         {
  97.             timeRes[count] = time[i];
  98.             arrWithRes[count] = arrWithData[i];
  99.             count++;
  100.         }
  101.     }
  102. }
  103.  
  104.  
  105. void Write(char* output, int& count, TimeVal*& time, double*& arrWithRes)
  106. {
  107.     fstream out(output, ios::out, ios::binary);
  108.  
  109.     if (out.fail())
  110.     {
  111.         cout << "couldn't open a file" << endl;
  112.         out.close();
  113.         exit(-1);
  114.     }
  115.     else
  116.     {
  117.         out.write((char*)&count, sizeof(int));
  118.         for (int i = 0; i < count; i++)
  119.         {
  120.             out.write((char*)&time[i], sizeof(TimeVal));
  121.             out.write((char*)&arrWithRes[i], sizeof(double));
  122.         }
  123.     }
  124.  
  125.     out.close();
  126. }
  127.  
  128. void BinInTxt(char* input, char* output)
  129. {
  130.     fstream in(input, ios::in, ios::binary);
  131.     fstream out(output, ios::out);
  132.  
  133.     if (in.fail() || out.fail())
  134.     {
  135.         cout << "couldn't open a file" << endl;
  136.         out.close();
  137.         in.close();
  138.         exit(-1);
  139.     }
  140.     else
  141.     {
  142.         int count;
  143.  
  144.         in.read((char*)&count, sizeof(int));
  145.  
  146.         out << count << endl;
  147.  
  148.         double* arrWithRes = new double[count];
  149.         TimeVal* time = new TimeVal[count];
  150.  
  151.         for (int i = 0; i < count; i++)
  152.         {
  153.             in.read((char*)&time[i], sizeof(TimeVal));
  154.             in.read((char*)&arrWithRes[i], sizeof(double));
  155.         }
  156.  
  157.         for (int i = 0; i < count; i++)
  158.         {
  159.             out.width(2);
  160.             out.fill('0');
  161.             out << time[i].h << ":";
  162.  
  163.             out.width(2);
  164.             out.fill('0');
  165.             out << time[i].m << ":";
  166.  
  167.             out.width(2);
  168.             out.fill('0');
  169.             out << time[i].s << " ";
  170.  
  171.             out << fixed << setprecision(1) << arrWithRes[i] << endl;
  172.         }
  173.  
  174.         delete[] arrWithRes;
  175.     }
  176.  
  177.     out.close();
  178.     in.close();
  179. }
  180.  
  181. int main()
  182. {
  183.     int n;
  184.     double threshold;
  185.  
  186.     int count;
  187.     TimeVal* time;
  188.     double* arrWithData = {};
  189.     TimeVal* timeRes;
  190.     double* arrWithRes = {};
  191.  
  192.     char input[] = "input.txt";
  193.     char output[] = "output.txt";
  194.     char inputb[] = "input.bin";
  195.     char outputb[] = "output.bin";
  196.  
  197.     TxtInBin(input, inputb);
  198.  
  199.     Read(inputb, n, time, arrWithData, threshold, count);
  200.     Res(threshold, n, time, arrWithData, count, timeRes, arrWithRes);
  201.     Write(outputb, count, timeRes, arrWithRes);
  202.  
  203.     BinInTxt(outputb, output);
  204.  
  205.     delete[] time;
  206.     delete[] arrWithData;
  207.     delete[] arrWithRes;
  208.     delete[] timeRes;
  209.  
  210.     system("pause");
  211.     return 0;
  212. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top