Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > streambuf

Reply
Thread Tools

streambuf

 
 
schultz
Guest
Posts: n/a
 
      02-24-2004
"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!


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-24-2004
schultz wrote:

> "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?

 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      02-24-2004
schultz wrote:
> 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.

--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::streambuf::setp and std::streambuf::epptr() Christopher Pisz C++ 2 12-12-2007 12:15 PM
std::streambuf::setg & std::streambuf::setp success or not? Peter Jansson C++ 1 11-08-2004 01:55 AM
Inheriting streambuf =?ISO-8859-1?Q?Viktor_Lundstr=F6m?= C++ 3 09-25-2003 10:35 AM
Problem with streambuf Matt Chaplain C++ 4 07-31-2003 12:30 PM
streambuf::underflow() infinite loop Davis King C++ 0 07-23-2003 02:37 AM



Advertisments