Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. struct BufferElement
  2. {
  3. char* ptrData;
  4. int TotalBytes;
  5. int StartPosition;
  6. std::string FileName;
  7. };
  8.  
  9. class FileThread
  10. {
  11. private:
  12. std::vector<BufferElement> Queue;
  13. bool BoolThread;
  14. std::ofstream writestream;
  15.  
  16. //This Method calls the "WriteInFile" Method for the first element in the
  17. //Queue and erases it from the vector
  18. void Execute( void )
  19. {
  20. while( BoolThread )
  21. {
  22. if( Queue.size() > 0 )
  23. {
  24. if( WriteInFile() )
  25. {
  26. delete[] Queue.at( 0 ).ptrData;
  27. Queue.erase( Queue.begin() );
  28. }
  29. }
  30. }
  31. }
  32.  
  33. //This Method writes the first Element of the Queue in the file
  34. bool WriteInFile( void )
  35. {
  36. if( Queue.at(0).ptrData == NULL )
  37. {
  38. return true;
  39. }
  40.  
  41. writestream.open( Queue.at(0).FileName.c_str(), std::ios::in |
  42. std::ios::out | std::ios::binary );
  43.  
  44. if( !writestream.is_open() )
  45. {
  46. writestream.close();
  47. writestream.clear();
  48. return false;
  49. }
  50.  
  51. writestream.seekp( Queue.at( 0 ).StartPosition );
  52. writestream.write( Queue.at( 0 ).ptrData, Queue.at( 0 ).TotalBytes );
  53.  
  54. writestream.close();
  55. writestream.clear();
  56.  
  57. return true;
  58. }
  59.  
  60. public:
  61. void EndThread(void)
  62. {
  63. BoolThread = false;
  64. for( int i = 0; i < Queue.size(); i++ )
  65. {
  66. delete[] Queue.at( i ).ptrData;
  67. }
  68. }
  69.  
  70. template< typename T >
  71. void WriteOrder( std::string _FileName, T _Data, int _StartPosition )
  72. {
  73. BufferElement Temp_BufferElement;
  74. Temp_BufferElement.TotalBytes = sizeof( _Data );
  75. Temp_BufferElement.StartPosition = _StartPosition;
  76. Temp_BufferElement.FileName = _FileName;
  77.  
  78. Temp_BufferElement.ptrData = new char[ Temp_BufferElement.TotalBytes ];
  79. memcpy( Temp_BufferElement.DataPtr, _Data, Temp_BufferElement.TotalBytes );
  80.  
  81. Queue.push_back( Temp_BufferElement );
  82. }
  83. };
  84.  
  85. int main(void)
  86. {
  87. std::string Path = "..\Data\Test.dat";
  88. FileThread Writer;
  89.  
  90. for( int i = 0; i < 1000; i++ )
  91. {
  92. char array[] = {'H','e','l','l','o',' ','W','o','r','l','d','!',''};
  93. Writer.WriteOrder( Path, array, i * sizeof( array );
  94. }
  95.  
  96. system("pause");
  97. Writer.EndThread();
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement