473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

streambuf in binary mode

Hello all,

I'm working on writing my own streambuf classes (to use in my custom
ostream/isteam classes that will handle reading/writing data to a
mmap'd file).

When reading from the mmap file, I essentially have a char buffer in my
streambuf class, that I'm registering with setp(). on an overflow()
call, I simply copy the contents of the buffer into the mmap'd file via
memcpy().

If I want to use this to write binary data via the streambuf classes
ie, are there any special considerations I need to be aware of in my
streambuf classes? Do I need to set any special flags to indicate that
the data is in binary mode perhaps? Any special precautions I need to
take, in overflow(int_type) for example?

My setup seems to work with binary data *MOST* of the time, however
there are rare occasions when there is inconsistency with the data i'm
writing and reading...

Any advice, comments would be much appreciated
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 23 '06 #1
7 6956
sm*******@excite.com wrote:
I'm working on writing my own streambuf classes (to use in my
custom ostream/isteam classes that will handle reading/writing
data to a mmap'd file).
When reading from the mmap file, I essentially have a char
buffer in my streambuf class, that I'm registering with
setp(). on an overflow() call, I simply copy the contents of
the buffer into the mmap'd file via memcpy().
That's not what I understand by mmap'd. I'd set the pointers
directly into the mmap'd file, and not use any additional
buffer. (Note that, at least under Unix, an mmap'd file cannot
grow. I have implemented a mmap'd streambuf in which overflow
unmapped the file, increased its size with truncate, and then
remapped it. Close could have truncated it to the last byte
actually written, but that wasn't necessary in my context.)
If I want to use this to write binary data via the streambuf
classes ie, are there any special considerations I need to be
aware of in my streambuf classes? Do I need to set any special
flags to indicate that the data is in binary mode perhaps? Any
special precautions I need to take, in overflow(int_type) for
example?
The binary option may be defined in ios_base, but it has no
meaning outside of std::basic_filebuf... or a user defined
streambuf, if the user so wants. In practice, a mmap'd
streambuf can only be used for binary files; it makes no sense
otherwise. Under Unix, of course, you can ignore the
distinction, because binary files and text files are identical.
So it's really up to you what you want to do: if you're only
targetting Unix machines, I'd just ignore it; if you also plan
to port to Windows or some other OS, I'd verify it, and reject
any open in which it isn't set.
My setup seems to work with binary data *MOST* of the time,
however there are rare occasions when there is inconsistency
with the data i'm writing and reading...
Not knowing your setup, nor even what OS you are using, it's
hard to say. As I said, mmap'd IO is inherently binary. If you
write mmap'd, and read through a filebuf opened in text mode, or
vice versa, you will have inconsistencies under most OS's.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 23 '06 #2
sm*******@excite.com wrote:
I'm working on writing my own streambuf classes (to use in my custom
ostream/isteam classes that will handle reading/writing data to a
mmap'd file).
You are doing it with streambuf because you use iostream formatted
input/output (<<,>>), don't you?

If you don't, you could use a much simpler interface, in order not to
deal with all the complexity of implementing std::streambuf. Something
like that:

struct stream
{
virtual ssize_t read(void*, size_t) = 0;
virtual ssize_t write(void const*, size_t) = 0;
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 23 '06 #3
sm*******@excite.com wrote:
If I want to use this to write binary data via the streambuf classes
ie, are there any special considerations I need to be aware of in my
streambuf classes? Do I need to set any special flags to indicate that
the data is in binary mode perhaps? Any special precautions I need to
take, in overflow(int_type) for example?
You do know the difference between "binary" mode in an iostream
and "formatted?".

All the binary flag does on the stream is turn off whatever line
end processing might be taking place (on Windows, \r\n -\n
conversion or vice versa). Formatted refers to using he
functions like << and >that convert the textual representation
to and from the operand types. Putting the stream in binary
momde doesn't change that. To "binary representations" of
these things, you use the unformatted I/O functions read
and write that just write an specified number of characters
to/from the stream.

All that being said, it is handled in the iostream base classes
and is totally transparent to the stream buffers. The stream
buffers just see a certain number of charT characters that
have already been formatted/new-line mapped.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 23 '06 #4
Ron Natalie wrote:
sm*******@excite.com wrote:
If I want to use this to write binary data via the streambuf
classes ie, are there any special considerations I need to
be aware of in my streambuf classes? Do I need to set any
special flags to indicate that the data is in binary mode
perhaps? Any special precautions I need to take, in
overflow(int_type) for example?
You do know the difference between "binary" mode in an
iostream and "formatted?".
All the binary flag does on the stream is turn off whatever
line end processing might be taking place (on Windows, \r\n ->
\n conversion or vice versa). Formatted refers to using he
functions like << and >that convert the textual
representation to and from the operand types. Putting the
stream in binary momde doesn't change that. To "binary
representations" of these things, you use the unformatted I/O
functions read and write that just write an specified number
of characters to/from the stream.
All that being said, it is handled in the iostream base
classes and is totally transparent to the stream buffers. The
stream buffers just see a certain number of charT characters
that have already been formatted/new-line mapped.
No. The iostream base classes are totally unaware of the
ios::binary flag, except that they declare it. In fact,
ios::binary is purely a streambuf issue; in the standard
library, the only class that uses it (other than to forward it)
is std::basic_filebuf.

Whether a user defined streambuf should use it or not depends on
what it does, but I suspect that cases where it should are very
rare. All it does is control the mapping between the file
representation and the memory representation of end of file and
end of line. There is already a streambuf class concerned with
reading and writing system files: basic_filebuf, and I can't
really think of a case where you would want another one.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 23 '06 #5

kanze wrote:
Whether a user defined streambuf should use it [ios::binary]
or not depends on what it does, but I suspect that cases
where it should are very rare.
Agreed.
All it does is control the mapping between the file
representation and the memory representation of end of file and
end of line. There is already a streambuf class concerned with
reading and writing system files: basic_filebuf, and I can't
really think of a case where you would want another one.
Surely whenever the streambuf needs to map between an internal format,
and an external binary format?

Examples I can think of are:
- writing multiline text into a windows text box (where you need \n
-\r\n conversion)
- writing text an HTTP GET request (which again I think needs \n >
CR, LF conversion)
- writing text to some network protocol which needs lines terminated
by ASCII LF (some Mac compilers use(d) '\n'==ASCII CR because that
allows ios::text to be a no-op for filebuf).

But, I agree it is rare.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 24 '06 #6
Martin Bonner wrote:
kanze wrote:
> All it does is control the mapping between the file
representation and the memory representation of end of file and
end of line. There is already a streambuf class concerned with
reading and writing system files: basic_filebuf, and I can't
really think of a case where you would want another one.
Surely whenever the streambuf needs to map between an internal format,
and an external binary format?

Examples I can think of are:
- writing multiline text into a windows text box (where you need \n
-\r\n conversion)
- writing text an HTTP GET request (which again I think needs \n >
CR, LF conversion)
- writing text to some network protocol which needs lines terminated
by ASCII LF (some Mac compilers use(d) '\n'==ASCII CR because that
allows ios::text to be a no-op for filebuf).

But, I agree it is rare.
If all or most of the rare cases where a streambuf class other than
basic_filebuf is needed are concerned with which character sequence to
use for end-of-line, maybe we could make it a parameter for a common
class so that we didn't have to reinvent the wheel for each case.
Has there been any discussion or proposal in the past?

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 24 '06 #7
Martin Bonner wrote:
kanze wrote:
Whether a user defined streambuf should use it [ios::binary]
or not depends on what it does, but I suspect that cases
where it should are very rare.
Agreed.
All it does is control the mapping between the file
representation and the memory representation of end of file and
end of line. There is already a streambuf class concerned with
reading and writing system files: basic_filebuf, and I can't
really think of a case where you would want another one.
Surely whenever the streambuf needs to map between an internal
format, and an external binary format?
You mean between the internal text format (stream of characters,
with end of line indicated by the character '\n') and an
external text format. The role of the binary flag is to turn
off a "default" mapping. (Sort of---it doesn't turn off the
locale specific mapping in filebuf, which makes its actual
semantics rather vague.)

Note that at present, it is *only* used in filebuf and the
[io]fstream; it is not used in the basic iostream idioms. This
means that anyone using it is aware of the derived type (filebuf
or the [io]fstream decorators). If you design a new streambuf
type, which needs different modes, it's up to you whether you
reuse std::ios::binary, or define your own mode options. In
general, I think I'd use std::ios::binary if the default mode
corresponded to some sort of text mapping (say converting lines
into separate records), and the other mode were something more
or less transparent.
Examples I can think of are:
- writing multiline text into a windows text box (where you need \n
-\r\n conversion)
Text formatting, in sum. But do you ever want to provide the
transparent mode?
- writing text an HTTP GET request (which again I think needs \n >
CR, LF conversion)
At a lower level. HTTP (application layer) is based on Internet
ASCII (presentation layer), at least in the header. In this
case, you do need the two modes, *but* you need to change them
dynamically---one mode for the header and other text data, and
the other for binary data.

Arguably, you might want a different mode for every filetype
handled. And of course, you'd want to ensure standard ASCII for
the header, but an encoding specified in the header for the
remaining text. Except that if the remaining text is HTML---a
relatively frequent case---it's also possible that the encoding
be specified in the <head>...</headsection of the document.

There are different ways of handling this, but a on/off switch
when opening the file isn't sufficient. (Of course, if all you
want to handle is the GET command, then there is only a header,
and you map '\n' to CRLF, without an option to not do so.)
- writing text to some network protocol which needs lines terminated
by ASCII LF (some Mac compilers use(d) '\n'==ASCII CR because that
allows ios::text to be a no-op for filebuf).
Again, either you don't want to support transparence, or you'll
likely have to support changing modes dynamically.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
2
by: Raf256 | last post by:
Hello, my custom streambuf works fine with output via << and with input via .get() but fails to input via >> or getline... any idea why? -------------------- A custom stream buffer (for...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
16
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.