streambuf

S

schultz

"File streams are associated to a buffer of type streambuf of certain size."

In what cases a file stream could have no associated buffer?
How to determine and resize the size of a buffer?
How to use manipulators, flush and endl to do synchronization?

Thanks!
 
R

Rolf Magnus

schultz said:
"File streams are associated to a buffer of type streambuf of certain
size."

In what cases a file stream could have no associated buffer?

According to the sentence you quote above, in none.
How to determine and resize the size of a buffer?

I don't think you can determine the size portably, but you can resize
the file by just writing beyond its end.
How to use manipulators, flush and endl to do synchronization?

Synchronitation with what?
 
D

Dietmar Kuehl

schultz said:
In what cases a file stream could have no associated buffer?

It is entirely the freedome of the person implementing the standard
C++ library. However, I'm not aware of any implementation which does
not have a buffer and I would expect any quality implementation to
have a buffer suitable for the underlying file system by default. Of
course, an implementation might choose to avoid a buffer when writing
to some form of special files, eg. named pipes, but this is entirely
out of the scope of the C++ standard.
How to determine and resize the size of a buffer?

There is no way to determine the size of a file buf's buffer other
than deriving from 'std::basic_filebuf' and inspecting the values
of the buffer pointers ('pbase()' and 'epptr()' for the put area and
'eback()' and 'egptr()' for the get area). However, you should probably
avoid fiddling explicitly with these pointers.

There is no portable way to guarantee a certain buffer size with the
exception of turning of buffering by calling 'pubsetbuf(0, 0)'. You
can feed a buffer and its size to the same function but the effect of
this call is open to the implementation: it can simply ignore the
request (unless both arguments are 0) which is exactly what I have done
in my implementation (because it is hard enough to getting file buffers
right when you can make at least a few basic assumptions...).
How to use manipulators, flush and endl to do synchronization?

When you are done writing and you want the bytes written to be sent to
the operating system, you should call 'pubsync()' on the underlying
file buffer in some form: both 'std::flush' and 'std::endl' will have
this effect. There are, however, also other means:

- you can call 'std::basic_ostream<...>::flush()'
- you can call 'std::basic_ios<...>::rdbuf()->pubsync()'
- you can set the 'std::ios_base::unitbuf' flag in the formatting flags
- you can read from a 'std::basic_ios<...>::tie()'d input stream

Note, that all of these will have the effect of flushing the put buffer's
content to the operating systems write function. There are a few things
to note:

- The buffer's content is converted according to the rules of the
corresponding 'std::codecvt<...>' facet.
- Only the output buffer is synchronized; there is no way to explicitly
synchronize the input buffer eg. to reflect changes made by another
process.
- The buffer is only written to the operating system and it is at the
discretion of the operating system when the data is sent to the
underlying representation, eg. when it is written to the disc. There
is no portable way to force immediate writing at the operating system
level.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top