Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstring>
  5. #include <vector>
  6. #include <time.h>
  7. #include <malloc.h>
  8. #include <algorithm>
  9. #include <string.h>
  10.  
  11. using namespace std;
  12.  
  13. class StopWatch
  14. {
  15. public:
  16. void start ();
  17. void stop ();
  18. void reset ();
  19. double seconds ();
  20. unsigned int milli_seconds ();
  21. private:
  22. time_t m_Start,
  23. m_Stop;
  24. };
  25.  
  26. void StopWatch::start ()
  27. {
  28. m_Start = clock ();
  29. }
  30.  
  31. void StopWatch::stop ()
  32. {
  33. m_Stop = clock ();
  34. }
  35.  
  36. void StopWatch::reset ()
  37. {
  38. m_Start = m_Stop = 0;
  39. }
  40.  
  41. double StopWatch::seconds ()
  42. {
  43. return double (m_Stop - m_Start) / CLOCKS_PER_SEC;
  44. }
  45.  
  46. unsigned int StopWatch::milli_seconds ()
  47. {
  48. return (double (m_Stop - m_Start) / CLOCKS_PER_SEC) * 1000;
  49. }
  50.  
  51. typedef std::vector<std::string> StringVector;
  52.  
  53. void writeBinary (StringVector& vec)
  54. {
  55. FILE* dest = fopen ("TextFileOutput.txt", "wb");
  56. if (dest == 0)
  57. {
  58. perror ("writeBinary ");
  59. return;
  60. }
  61. for (const std::string& s : vec)
  62. {
  63. fwrite (s.c_str (), strlen (s.c_str ()), 1, dest);
  64. }
  65. fclose (dest);
  66. }
  67.  
  68. int main ()
  69. {
  70. //StringVector v (5000000, "ö xin chào các bạn, các bạn khỏa không nè\n");
  71. //writeBinary (v);
  72.  
  73.  
  74. StopWatch sw;
  75.  
  76. // cout << getFileSize("a1.txt");
  77.  
  78.  
  79.  
  80. //StringVector v;
  81. sw.start();
  82. //while (true)
  83.  
  84. std::fstream ifstream;
  85. std::fstream ofstream;
  86.  
  87. ifstream.open("b.txt", std::ios::in | std::ios::binary);
  88. bool isBreak = false;
  89. string s;
  90. vector<string> v;
  91. int currentIndexFile = 0;
  92. int maxBufferSize = 50000000;
  93. int currentSize = 0;
  94.  
  95. char fileName[10];
  96. vector<string*> myLst;
  97. while (ifstream)
  98. {
  99. std::getline(ifstream, s);
  100. if (currentSize + s.length() > maxBufferSize)
  101. {
  102. snprintf(fileName, sizeof(fileName), "%d.txt", currentIndexFile++);
  103. ofstream.open(fileName, std::ios::out | std::ios::binary);
  104. sort(myLst.begin(), myLst.end());
  105.  
  106. for (int i = 0; i < myLst.size(); i++)
  107. {
  108. ofstream << *myLst[i] << endl;
  109. free(myLst[i]);
  110. }
  111. ofstream.close();
  112.  
  113. currentSize = 0;
  114. myLst.clear();
  115. }
  116. currentSize += s.length();
  117. myLst.push_back(&s);
  118. //ofstream << s << endl;
  119. }
  120.  
  121. sw.stop ();
  122.  
  123. std::cout << "\n\nTime for read binary file: " << sw.milli_seconds () << " ms";
  124. std::cout << "\n\n";
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement