Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cstring>
- #include <vector>
- #include <time.h>
- #include <malloc.h>
- #include <algorithm>
- #include <string.h>
- using namespace std;
- class StopWatch
- {
- public:
- void start ();
- void stop ();
- void reset ();
- double seconds ();
- unsigned int milli_seconds ();
- private:
- time_t m_Start,
- m_Stop;
- };
- void StopWatch::start ()
- {
- m_Start = clock ();
- }
- void StopWatch::stop ()
- {
- m_Stop = clock ();
- }
- void StopWatch::reset ()
- {
- m_Start = m_Stop = 0;
- }
- double StopWatch::seconds ()
- {
- return double (m_Stop - m_Start) / CLOCKS_PER_SEC;
- }
- unsigned int StopWatch::milli_seconds ()
- {
- return (double (m_Stop - m_Start) / CLOCKS_PER_SEC) * 1000;
- }
- typedef std::vector<std::string> StringVector;
- void writeBinary (StringVector& vec)
- {
- FILE* dest = fopen ("TextFileOutput.txt", "wb");
- if (dest == 0)
- {
- perror ("writeBinary ");
- return;
- }
- for (const std::string& s : vec)
- {
- fwrite (s.c_str (), strlen (s.c_str ()), 1, dest);
- }
- fclose (dest);
- }
- int main ()
- {
- //StringVector v (5000000, "ö xin chào các bạn, các bạn khỏa không nè\n");
- //writeBinary (v);
- StopWatch sw;
- // cout << getFileSize("a1.txt");
- //StringVector v;
- sw.start();
- //while (true)
- std::fstream ifstream;
- std::fstream ofstream;
- ifstream.open("b.txt", std::ios::in | std::ios::binary);
- bool isBreak = false;
- string s;
- vector<string> v;
- int currentIndexFile = 0;
- int maxBufferSize = 50000000;
- int currentSize = 0;
- char fileName[10];
- vector<string*> myLst;
- while (ifstream)
- {
- std::getline(ifstream, s);
- if (currentSize + s.length() > maxBufferSize)
- {
- snprintf(fileName, sizeof(fileName), "%d.txt", currentIndexFile++);
- ofstream.open(fileName, std::ios::out | std::ios::binary);
- sort(myLst.begin(), myLst.end());
- for (int i = 0; i < myLst.size(); i++)
- {
- ofstream << *myLst[i] << endl;
- free(myLst[i]);
- }
- ofstream.close();
- currentSize = 0;
- myLst.clear();
- }
- currentSize += s.length();
- myLst.push_back(&s);
- //ofstream << s << endl;
- }
- sw.stop ();
- std::cout << "\n\nTime for read binary file: " << sw.milli_seconds () << " ms";
- std::cout << "\n\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement