Topic: Control of char * I/O
Author: bkline@cortex.nlm.nih.gov (Bob Kline Phoenix Contract)
Date: 1995/07/03 Raw View
The standard C I/O package provides a convenient method for controlling
the maximum number of characters to write in formatted I/O, e.g.:
fprintf(fp, "FONT NAME: %.16s\n", font_desc.font_name);
This handles the case of a structure which has enough space for
a string which will not necessarily be NUL-terminated if the
maximum number of characters are stored for the string (a common
enough situation when one is reading in data structures written
by someone else's software).
What are the chances of getting such a mechanism added to the
iostreams package? (Or is it there already and I just don't know
where to look for it?)
--
/*----------------------------------------------------------------------*/
/* Bob Kline Stream International */
/* bob_kline@stream.com formerly Corporate Software, Inc. */
/* voice: (703) 522-0820 x-311 fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/03 Raw View
In article 26172@nlm.nih.gov, bkline@cortex.nlm.nih.gov (Bob Kline Phoenix Contract) writes:
>The standard C I/O package provides a convenient method for controlling
>the maximum number of characters to write in formatted I/O, e.g.:
>
> fprintf(fp, "FONT NAME: %.16s\n", font_desc.font_name);
It would seem reasonable for the "precision" specification to control
the maximum number of characters written for a char*, as it does
for printf. That wasn't part of the original iostream specification,
and isn't in the draft standard, however. Maybe it could be added.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: tholaday@jpmorgan.com (Thomas Holaday,COMM)
Date: 1995/07/03 Raw View
>The standard C I/O package provides a convenient method for controlling
>the maximum number of characters to write in formatted I/O, e.g.:
>
> fprintf(fp, "FONT NAME: %.16s\n", font_desc.font_name);
In order to get the same effect with iostreams, I have found the following
to be successful:
extern ofstream &fp;
fp << "FONT NAME: " << setw(16) << font_desc.font_name << '\n' ;
I'm a bit worried that Steve Clamage was thinking of extending the meaning of
precision, though, because maybe my stuff has been working due to a hithertoo
unsuspected vendor extension.
---
~THol() Thomas Holaday
holaday_thomas@jpmorgan.com tlhol@ibm.net
70407.534@compuserve.com
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/03 Raw View
In article 5qa@hardcopy.ny.jpmorgan.com, tholaday@jpmorgan.com (Thomas Holaday,COMM) writes:
>>The standard C I/O package provides a convenient method for controlling
>>the maximum number of characters to write in formatted I/O, e.g.:
>>
>> fprintf(fp, "FONT NAME: %.16s\n", font_desc.font_name);
>
>In order to get the same effect with iostreams, I have found the following
>to be successful:
>
> extern ofstream &fp;
> fp << "FONT NAME: " << setw(16) << font_desc.font_name << '\n' ;
The "width" attribute is supposed to set the MINIMUM field width for output,
just as with printf. If your implemenation uses it to truncate output,
that violates all iostream documentation I am familiar with.
>I'm a bit worried that Steve Clamage was thinking of extending the meaning of
>precision, though, because maybe my stuff has been working due to a hithertoo
>unsuspected vendor extension.
The "precision" attribute presently has no meaning for char* output,
and printf has always used it to truncate char* output. Adding that
meaning to iostream char* output might break existing code, since
"precision" is not reset by each output operation. This idea needs to be
evaluated in more detail.
---
Steve Clamage, stephen.clamage@eng.sun.com