home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18857 < prev    next >
Encoding:
Text File  |  1993-01-08  |  1.7 KB  |  52 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!psinntp!dg-rtp!webo!bhaskara.webo.dg.com!pramesh
  3. From: pramesh@bhaskara.webo.dg.com (Ramesh Babu)
  4. Subject: Re: How to limit string output length w/ stream manips?
  5. Sender: usenet@webo.dg.com (Usenet Administration)
  6. Message-ID: <1993Jan8.211251.4604@webo.dg.com>
  7. Date: Fri, 8 Jan 93 21:12:51 GMT
  8. Reply-To: pramesh@bhaskara.webo.dg.com
  9. References: <1993Jan8.025836.10039@news.ysu.edu> <1993Jan8.183239.10873@taumet.com>
  10. Organization: NSDD, Data General Corp.
  11. Lines: 39
  12.  
  13. In article <1993Jan8.183239.10873@taumet.com>, steve@taumet.com (Steve Clamage) writes:
  14. |> af458@yfn.ysu.edu (Jon K. Salmon) writes:
  15. |> 
  16. |> 
  17. |> >I would like to limit the output length of a string sent to an
  18. |> >output stream.  For example, given a string...
  19. |> 
  20. |> >  const char *Bob = "1234567890ABCDEF";
  21. |> 
  22. |> > ... to output "12345"?
  23. |> 
  24. |> As with printf(), the iostream inserters never truncate a value; output
  25. |> width is a minimum, not an absolute.
  26. |> 
  27. |> If you want to truncate a string, you have to do the truncation yourself.
  28. |> -- 
  29. |> 
  30. |> Steve Clamage, TauMetric Corp, steve@taumet.com
  31.  
  32. In C printf with a format string, the above functionality can be
  33. implemented. Refer to pg 153 of K&R 2nd edition.
  34.  
  35. Example:
  36.  
  37.       printf ( "%2.2s\n", "ABCD" );
  38.  
  39. will output just
  40.  
  41.       AB
  42. in "%2.2" the first 2 specifies the min length and the second 2 
  43. the max length. The first 2 can be omitted then the format string
  44. becomes "%.2" will have the same desired results.
  45.  
  46. ***********************************************************************
  47. Ramesh babu P                   pramesh@ox.webo.dg.com
  48. Data General Corp.
  49. Westboro, Ma (508)-870-8048      Fax (508)-898-4212
  50. ***********************************************************************
  51.  
  52.