Board index » cppbuilder » winapi WriteFile single byte and buffering

winapi WriteFile single byte and buffering


2008-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.
 
 

Re:winapi WriteFile single byte and buffering

Hi John
John Grabner says:
Quote
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.
An idea:
class TBufferWrite
{
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
 

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
Quote
John Grabner wrote in message
news:47859f0a$ XXXX@XXXXX.COM ...

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?
 

{smallsort}

Re:winapi WriteFile single byte and buffering

John Grabner wrote:
Quote
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.
I believe Windows defaults to buffered writes.
You can turn +off+ buffered writes with these
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