Below is the sample piece of code from the existing project.
Code links the file descriptor to a file_stream. And using that file_stream to write data into disk. (so that we can use "<<" operators).
class fd_streambuf : public std::streambuf {
....
};
void func()
{
int m_logFd = open("fileName", FLAG);
std::auto_ptr<ostream> outStream = new fd_streambuf(m_logFd);
int idx = 0;
while (idx++<1000){
outStream << " A REALLY LARGE DATA \n";
}
close(m_logFd);
}
The code is working as expected.
My doubt is, linking the file descriptor with ostream.
DOES IT CAUSE ANY PERFORMANCE IMPACT?
how often data will written to disk?
I do not want too many disk write operation.
I am trying to implement fully buffered stream. is it possible to do it by linking file descriptor with stream.
Or, shall i use ofstream, instead of open() and linking the file descriptor. which one will perform well?
There probably isn't a different performance wise or in functionality between what you are doing and what you are
suggesting. Streams are by default line buffered: data is written back to disk at all of the following points: new line
seen, internal buffer has filled, explicit flush, stream close.
I would look at turning off the line-oriented nature of the stream and possibly increasing the size of the internal
buffer, both of which would seem to me to be possible with simple function calls after you open the stream.