home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / istngstr.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  4KB  |  149 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /***************************************************************************
  4.  *
  5.  * istringstream.cpp - basic_istringstream example
  6.  *
  7.  * $Id: istringstream.cpp,v 1.6 1996/09/26 23:33:21 philippe Exp $
  8.  *
  9.  ***************************************************************************
  10.  *
  11.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  12.  * ALL RIGHTS RESERVED *
  13.  * The software and information contained herein are proprietary to, and
  14.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  15.  * intends to preserve as trade secrets such software and information.
  16.  * This software is furnished pursuant to a written license agreement and
  17.  * may be used, copied, transmitted, and stored only in accordance with
  18.  * the terms of such license and with the inclusion of the above copyright
  19.  * notice.  This software and information or any other copies thereof may
  20.  * not be provided or otherwise made available to any other person.
  21.  *
  22.  * Notwithstanding any other lease or license that may pertain to, or
  23.  * accompany the delivery of, this computer software and information, the
  24.  * rights of the Government regarding its use, reproduction and disclosure
  25.  * are as set forth in Section 52.227-19 of the FARS Computer
  26.  * Software-Restricted Rights clause.
  27.  * 
  28.  * Use, duplication, or disclosure by the Government is subject to
  29.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  30.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  31.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  32.  * P.O. Box 2328, Corvallis, Oregon 97339.
  33.  *
  34.  * This computer software and information is distributed with "restricted
  35.  * rights."  Use, duplication or disclosure is subject to restrictions as
  36.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  37.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  38.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  39.  * then the "Alternate III" clause applies.
  40.  *
  41.  **************************************************************************/
  42.  
  43. //
  44. // stdlib/examples/manual/istringstream.cpp
  45. //
  46. #include<iostream>
  47. #include<sstream>
  48. #include<string>
  49. #include<iomanip>
  50.  
  51.  #ifndef _RWSTD_NO_NAMESPACE
  52.   using namespace std;
  53.  #endif 
  54.  
  55. int main ( )
  56. {
  57.   long   l= 20;
  58.  
  59. #ifndef _RWSTD_NO_WIDE_CHAR
  60.   wchar_t *ntbs=L"Il avait l'air heureux";
  61.   wchar_t c;
  62.   wchar_t buf[50];
  63.  
  64.   // create a read/write string-stream object on wide char
  65.   // and attach it to an wistringstream object
  66.   wistringstream in(ios_base::in | ios_base::out);
  67.  
  68.   // tie the ostream object to the wistringstream object
  69.   wostream out(in.rdbuf());   
  70.  
  71.   // output ntbs in out
  72.   out << ntbs;
  73.  
  74.   // output each word on a separate line
  75.   while ( in.get(c) )
  76.    {
  77.      if ( wistringstream::traits_type::eq(c,L' ') ) 
  78.       wcout << endl;
  79.      else
  80.       wcout << c;
  81.    }
  82.   wcout << endl << endl;
  83.  
  84.   // move back the input sequence to the beginning
  85.   in.seekg(0); 
  86.  
  87.   // clear the state flags
  88.   in.clear();
  89.  
  90.   // does the same thing as the previous code
  91.   // output each word on a separate line
  92.   while ( in >> buf )
  93.    wcout << buf << endl; 
  94.     
  95.   wcout << endl << endl;
  96. #endif
  97.  
  98.   // create a tiny string object
  99.   string test_string("Il dormait pour l'eternite");
  100.  
  101.   // create a read/write string-stream object on char
  102.   // and attach it to an istringstream object
  103.   istringstream in_bis(ios_base:: in | ios_base::out |
  104.                        ios_base::app );
  105.  
  106.   // create an ostream object
  107.   ostream out_bis(in_bis.rdbuf());  
  108.  
  109.   // initialize the string-buffer with test_string
  110.   in_bis.str(test_string);
  111.  
  112.   out_bis << endl;
  113.  
  114.   // output the base info before each integer
  115.   out_bis << showbase;
  116.  
  117.   ostream::pos_type pos= out_bis.tellp();
  118.  
  119.   // output l in hex with a field with of 20 
  120.   out_bis << hex << setw(20) << l << endl;
  121.  
  122.   // output l in oct with a field with of 20
  123.   out_bis << oct << setw(20) << l << endl;
  124.  
  125.   // output l in dec with a field with of 20
  126.   out_bis << dec << setw(20) << l << endl;
  127.  
  128.   // output the all buffer
  129.   cout << in_bis.rdbuf();
  130.  
  131.   // seek the input sequence to pos  
  132.   in_bis.seekg(pos);
  133.  
  134.   int a,b,d;
  135.  
  136.   in_bis.unsetf(ios_base::basefield);
  137.  
  138.   // read the previous outputted integer
  139.   in_bis >> a >> b >> d;
  140.  
  141.   // output 3 times 20
  142.   cout << a << endl << b << endl << d << endl;
  143.  
  144.   return 0;
  145.  }
  146.  
  147.  
  148.  
  149.