Board index » cppbuilder » winapi WriteFile single byte and buffering
John Grabner
![]() CBuilder Developer |
John Grabner
![]() CBuilder Developer |
winapi WriteFile single byte and buffering2008-01-10 12:27:28 PM cppbuilder81 I am using WrtieFile to write a lot of single bytes to a file. Reading the documentation I cannot work out if I can have a buffered write and if so how do to do a buffered write. Any one know the answer? John. |
Asger Joergensen
![]() CBuilder Developer |
2008-01-10 06:18:32 PM
Re:winapi WriteFile single byte and buffering
Hi John
John Grabner says: QuoteI am using WrtieFile to write a lot of single bytes to a file. Reading { AnsiString FBuffer;// or another type of buffer int FMaxBuffer, FBufferSize; HANDLE hFile; void __fastcall WriteBufferToFile(){FBufferSize = 0;}; public: __fastcall TBufferWrite() : FMaxBuffer(0) , hFile(0) , FBufferSize(0){} __fastcall ~TBufferWrite(){/*Close the File if open*/} bool __fastcall OpenFile(AnsiString FileName); bool __fastcall WriteFile(char aChar) { ++FBufferSize; FBuffer += aChar; if(FBufferSize>= FMaxBuffer) WriteBufferToFile(); } void __fastcall CloseFile(); }; Code not tested in anyway.. Kind regards Asger |
Ed Mulroy [TeamB]
![]() CBuilder Developer |
2008-01-10 11:55:58 PM
Re:winapi WriteFile single byte and buffering
WriteFile is an operating system function and buffering for it is up to the
OS. Your only controls over buffering are from specifying one of FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING in the call to CreateFile. Both of them limit or remove the effects of buffering. However without them, the OS still determines what buffering it will provide so is free to decide what size buffer to use including using no buffer. Typically the writes to a hard drive are buffered for performance. For more detail read the help file documentation on CreateFile. . Ed QuoteJohn Grabner wrote in message {smallsort} |
Bob Gonder
![]() CBuilder Developer |
2008-01-11 01:08:18 AM
Re:winapi WriteFile single byte and buffering
John Grabner wrote:
QuoteI am using WrtieFile to write a lot of single bytes to a file. Reading two flags when you use CreateFile(): FILE_FLAG_WRITE_THROUGH FILE_FLAG_NO_BUFFERING So, in your case, make sure they are off. In addition, you might want to turn on FILE_FLAG_SEQUENTIAL_SCAN |