Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct BufferElement
- {
- char* ptrData;
- int TotalBytes;
- int StartPosition;
- std::string FileName;
- };
- class FileThread
- {
- private:
- std::vector<BufferElement> Queue;
- bool BoolThread;
- std::ofstream writestream;
- //This Method calls the "WriteInFile" Method for the first element in the
- //Queue and erases it from the vector
- void Execute( void )
- {
- while( BoolThread )
- {
- if( Queue.size() > 0 )
- {
- if( WriteInFile() )
- {
- delete[] Queue.at( 0 ).ptrData;
- Queue.erase( Queue.begin() );
- }
- }
- }
- }
- //This Method writes the first Element of the Queue in the file
- bool WriteInFile( void )
- {
- if( Queue.at(0).ptrData == NULL )
- {
- return true;
- }
- writestream.open( Queue.at(0).FileName.c_str(), std::ios::in |
- std::ios::out | std::ios::binary );
- if( !writestream.is_open() )
- {
- writestream.close();
- writestream.clear();
- return false;
- }
- writestream.seekp( Queue.at( 0 ).StartPosition );
- writestream.write( Queue.at( 0 ).ptrData, Queue.at( 0 ).TotalBytes );
- writestream.close();
- writestream.clear();
- return true;
- }
- public:
- void EndThread(void)
- {
- BoolThread = false;
- for( int i = 0; i < Queue.size(); i++ )
- {
- delete[] Queue.at( i ).ptrData;
- }
- }
- template< typename T >
- void WriteOrder( std::string _FileName, T _Data, int _StartPosition )
- {
- BufferElement Temp_BufferElement;
- Temp_BufferElement.TotalBytes = sizeof( _Data );
- Temp_BufferElement.StartPosition = _StartPosition;
- Temp_BufferElement.FileName = _FileName;
- Temp_BufferElement.ptrData = new char[ Temp_BufferElement.TotalBytes ];
- memcpy( Temp_BufferElement.DataPtr, _Data, Temp_BufferElement.TotalBytes );
- Queue.push_back( Temp_BufferElement );
- }
- };
- int main(void)
- {
- std::string Path = "..\Data\Test.dat";
- FileThread Writer;
- for( int i = 0; i < 1000; i++ )
- {
- char array[] = {'H','e','l','l','o',' ','W','o','r','l','d','!','