Topic: Using iostreams to output to memory
Author: "David" <nospam@NOatl-SPAMintl.com>
Date: 1999/04/11 Raw View
But the source is not always a char string, so simply copying is not
adequate to maintain type-independent code. Were the old form used,
"sbuf << something" would work regardless of the type of "something".
David
sNtOdScPpApM@NaOtSlP-iAnMt!l.com
remove the anti-spam junk to respond ('l' is lower-case 'L')
Matt Seitz wrote in message <37052833.0@newsread.exodus.net>...
>Max Polk wrote in message ...
>>For example, how can we use iostreams to format name and address here:
>>
>>struct Out
>>{
>> char name[20];
>> char address[20];
>>};
>>
>>If the answer is "use ostringstream", then let me point out that the
>>following is extremely awkward and inefficient:
>>
>>fillIn (Out &out, string &name)
>>{
>> ostringstream t;
>>
>> t.width (sizeof (out.name));
>>
>> t << name;
>>
>> memcpy (out.name, t.str ().c_str (), sizeof (out.name));
>>}
>
>Don't use iostreams, just use string functions:
>void fillIn (Out &out, string const & name)
>{
> string::size_type const length = name.copy(out.name,
>sizeof(out.name)-sizeof('\0'));
> out.name[length] = '\0';
>}
>
>
>}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: nospam@nospam.com
Date: 1999/04/11 Raw View
The source is not always a char string, so simply copying is not
adequate to maintain type-independent code. Were the old form used,
"sbuf << something" would work regardless of the type of "something".
A major point of object-oriented design is to minimize the sensitivity
to type changes.
David
sNtOdScPpApM@NaOtSlP-iAnMt!l.com
remove the anti-spam junk to respond ('l' is lower-case 'L')
Matt Seitz wrote in message <37052833.0@newsread.exodus.net>...
>Max Polk wrote in message ...
>>For example, how can we use iostreams to format name and address here:
>>
>>struct Out
>>{
>> char name[20];
>> char address[20];
>>};
>>
>>If the answer is "use ostringstream", then let me point out that the
>>following is extremely awkward and inefficient:
>>
>>fillIn (Out &out, string &name)
>>{
>> ostringstream t;
>>
>> t.width (sizeof (out.name));
>>
>> t << name;
>>
>> memcpy (out.name, t.str ().c_str (), sizeof (out.name));
>>}
>
>Don't use iostreams, just use string functions:
>void fillIn (Out &out, string const & name)
>{
> string::size_type const length = name.copy(out.name,
>sizeof(out.name)-sizeof('\0'));
> out.name[length] = '\0';
>}
>
>
>}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: none@none.invalid (Max Polk)
Date: 1999/04/02 Raw View
Since ostrstring was deprecated, how do implementors of the C++ Standard
library expect its users to use iostreams to output to user-allocated
memory?
For example, how can we use iostreams to format name and address here:
struct Out
{
char name[20];
char address[20];
};
If the answer is "use ostringstream", then let me point out that the
following is extremely awkward and inefficient:
fillIn (Out &out, string &name)
{
ostringstream t;
t.width (sizeof (out.name));
t << name;
memcpy (out.name, t.str ().c_str (), sizeof (out.name));
}
I have to get the formatted string with str(), then I have to get at the
bytes in it with c_str(), then I am forced to use memcpy (or
traits::copy) to copy these bytes into the structure.
The above fills in a single field in this structure. If the structure
has 20 or 100 char arrays, how are we now to use the C++ standard library
efficiently? Have to call str(), then c_str(), then memcpy() seems like
a monumental waste of time and potentially several intermediate objects
being created.
Author: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1999/04/02 Raw View
Max Polk wrote in message ...
>For example, how can we use iostreams to format name and address here:
>
>struct Out
>{
> char name[20];
> char address[20];
>};
>
>If the answer is "use ostringstream", then let me point out that the
>following is extremely awkward and inefficient:
>
>fillIn (Out &out, string &name)
>{
> ostringstream t;
>
> t.width (sizeof (out.name));
>
> t << name;
>
> memcpy (out.name, t.str ().c_str (), sizeof (out.name));
>}
Don't use iostreams, just use string functions:
void fillIn (Out &out, string const & name)
{
string::size_type const length = name.copy(out.name,
sizeof(out.name)-sizeof('\0'));
out.name[length] = '\0';
}
}
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]