Advertisement
vlad_davydov

TestTask

Jul 11th, 2021 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5.  
  6. using std::ifstream;
  7. using std::ofstream;
  8. typedef int32_t number_type;    // тип в котором будут хранится числа в бинарном файле
  9. // (также определяет диапазон значений)
  10. int fromTXTtoBIN(ifstream & in, ofstream & out);
  11. int fromBINtoTXT(ifstream & in, ofstream & out);
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     ifstream txtfile("some.txt");
  16.     ofstream binfile("some.bin");
  17.     std::cout<<fromTXTtoBIN(txtfile, binfile)<<std::endl;
  18.  
  19.     txtfile.close();
  20.     binfile.close();
  21.  
  22.     ofstream txtfile2("sometxtfrombin.txt");
  23.     ifstream binfile2("some.bin");
  24.     std::cout<<fromBINtoTXT(binfile2, txtfile2)<<std::endl;
  25.  
  26.     txtfile2.close();
  27.     binfile2.close();
  28.  
  29.     return 0;
  30.  
  31.  
  32. }
  33.  
  34. int fromTXTtoBIN(ifstream & in, ofstream & out)
  35. {
  36.     std::stringstream ss;
  37.     std::string s;
  38.     float num;
  39.     size_t numbers_convert = 0;
  40.     bool newline=false;
  41.     if (in.is_open() && out.is_open())
  42.     {
  43.         while(std::getline(in, s) && ( !s.empty() || !in.eof()) )
  44.         {
  45.             if(newline)
  46.                 out<<std::endl;
  47.             ss.str(s);
  48.             while(ss>>num){
  49.                 out.write(reinterpret_cast<const char*>(&num), sizeof(num));
  50.                 ++numbers_convert;
  51.             }
  52.             newline = true;
  53.             ss.str("");
  54.             ss.clear();
  55.             s.clear();
  56.         }
  57.         return numbers_convert;
  58.     }
  59.     return -1; // ошибка открытия файлов
  60. }
  61. int fromBINtoTXT(ifstream & in, ofstream & out)
  62. {
  63.     std::stringstream ss;
  64.     std::string s;
  65.     float num=0;
  66.     size_t numbers_convert = 0;
  67.     bool newline=false, space = false;
  68.     if (in.is_open() && out.is_open())
  69.     {
  70.         while(std::getline(in, s) && ( !s.empty() || !in.eof()) )
  71.         {
  72.             if(newline)
  73.                 out<<std::endl;
  74.             ss.str(s);
  75.             space = false;
  76.             while(ss.read(reinterpret_cast<char*>(&num), sizeof(num))){
  77.                 if (space) out<<' ';
  78.                 out<<num;
  79.                 ++numbers_convert;
  80.                 space = true;
  81.             }
  82.             newline = true;
  83.             ss.str("");
  84.             ss.clear();
  85.             s.clear();
  86.         }
  87.         return numbers_convert;
  88.     }
  89.     return -1; // ошибка открытия файлов
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement