home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 20061 < prev    next >
Encoding:
Text File  |  1993-01-28  |  2.6 KB  |  97 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!charon.amdahl.com!amdahl!rtech!pacbell.com!ames!agate!stanford.edu!lucid.com!lucid.com!jss
  3. From: jss@lucid.com (Jerry Schwarz)
  4. Subject: Re: buffering iostreams
  5. Message-ID: <1993Jan28.034002.5892@lucid.com>
  6. Sender: usenet@lucid.com
  7. Reply-To: jss@lucid.com (Jerry Schwarz)
  8. Organization: Lucid, Inc.
  9. References:  <1k2co6INN7mm@wilma.cs.widener.edu>
  10. Date: Thu, 28 Jan 93 03:40:02 GMT
  11. Lines: 84
  12.  
  13. In article <1k2co6INN7mm@wilma.cs.widener.edu>, quairoli@cs.widener.edu (Patrick J. Quairoli) writes:
  14. |> i'm trying to take a long (i.e. 72) and cast it as a char so
  15. |> that i get a value of 'H'.  this can be easily done by:
  16. |> 
  17. |> long foo = 72;
  18. |> 
  19. |> cout << (char)foo;
  20. |> 
  21. |> BUT!  i also want the output to be buffered as an long (4 bytes).
  22. |> 
  23.  
  24. First, what you're talking about is generally called "padding"
  25. not "buffering", and long's aren't normally padded to 4 characters
  26. either.  
  27.  
  28. |> 
  29. |> cout << setw(sizeof(long)) << (char)foo;
  30. |> 
  31. |> which gives me 'H' but it is not buffered (i.e. its' flush with the
  32. |> left of the screen and there are no trialing spaces). if it were
  33. |> buffered as a long it would be 'H   '.
  34. |> 
  35.  
  36. Much of the documentation of iostreams says that the char inserter
  37. pads. But the original AT&T library didn't and most (maybe all)
  38. of the other implementations have followed the original implementation
  39. (rather than the original documentation). 
  40.  
  41. The standards committee will probably go along with the existing
  42. implementations rather than the original documentation and say
  43. that the char inserter doesn't pad either.
  44.  
  45. |> if i use:
  46. |> 
  47. |> cout << setw(sizeof(long)) << foo;
  48. |> 
  49. |> i get 72 and it is buffered with 2 leading spaces (i.e. two bytes)....
  50. |> 
  51. |> ? how do i get a long to be cast as a char and still buffer it?
  52. |> 
  53.  
  54. One way is to use a string rather than a char.
  55.  
  56.     char a[2] ;
  57.     a[0] = foo ;
  58.     a[1] = 0 ;
  59.     cout << setw(sizeof(long)) << a 
  60.  
  61. If you you're going to be doing this much, you might find it convenient
  62. to wrap it up in a manipulator.
  63.  
  64.     #include <iostream.h>
  65.     #include <iomanip.h>
  66.  
  67.     ostream& paddedcharf(ostream& o, int c) {
  68.         char a[2] ;
  69.         a[0] = c ;
  70.         a[1] = 0 ;
  71.         return o << setw(sizeof(long)) << a ;
  72.     }
  73.  
  74.     OAPP(int) paddedchar(paddedcharf) ;  
  75.  
  76.     ...
  77.     cout << paddedchar(foo) ;
  78.  
  79. I used an "int" manipulator rather than a "long" (as might
  80. be suggested by the original problem) because iomanip.h declares
  81. it. 
  82.  
  83. And finally with regard to whether padding comes before
  84. or after the value.  This can be controlled by flags.  To get
  85. the padding after the data instead of before (the default is
  86. before) you do
  87.  
  88.     cout.setf(ios::left,ios::adjustfield) ;
  89.  
  90. The flag specifies where the data goes.
  91.  
  92.    -- Jerry Schwarz
  93.  
  94.  
  95.  
  96.  
  97.