home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Examples / StdLib / istngstr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  4.3 KB  |  146 lines

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /***************************************************************************
  4.  *
  5.  * istringstream.cpp - basic_istringstream example
  6.  *                     See Class Reference Section
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * (c) Copyright 1994, 1998 Rogue Wave Software, Inc.
  11.  * ALL RIGHTS RESERVED
  12.  *
  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. #include<iostream>
  44. #include<sstream>
  45. #include<string>
  46. #include<iomanip>
  47.  
  48. int main ( )
  49. {
  50. #ifndef _RWSTD_NO_NAMESPACE
  51.   using namespace std;
  52. #endif 
  53.  
  54.   long   l= 20;
  55.  
  56. #ifndef _RWSTD_NO_WIDE_CHAR
  57.   wchar_t *ntbs=L"Il avait l'air heureux";
  58.   wchar_t c;
  59.   wchar_t buf[50];
  60.  
  61.   // create a read/write string-stream object on wide char
  62.   // and attach it to an wistringstream object
  63.   wistringstream in(ios_base::in | ios_base::out);
  64.  
  65.   // tie the ostream object to the wistringstream object
  66.   wostream out(in.rdbuf());   
  67.  
  68.   // output ntbs in out
  69.   out << ntbs;
  70.  
  71.   // output each word on a separate line
  72.   while ( in.get(c) )
  73.    {
  74.      if ( wistringstream::traits_type::eq(c,L' ') ) 
  75.       wcout << endl;
  76.      else
  77.       wcout << c;
  78.    }
  79.   wcout << endl << endl;
  80.  
  81.   // move back the input sequence to the beginning
  82.   in.seekg(0); 
  83.  
  84.   // clear the state flags
  85.   in.clear();
  86.  
  87.   // does the same thing as the previous code
  88.   // output each word on a separate line
  89.   while ( in >> buf )
  90.    wcout << buf << endl; 
  91.     
  92.   wcout << endl << endl;
  93. #endif
  94.  
  95.   // create a tiny string object
  96.   string test_string("Il dormait pour l'eternite");
  97.  
  98.   // create a read/write string-stream object on char
  99.   // and attach it to an istringstream object
  100.   istringstream in_bis(ios_base:: in | ios_base::out |
  101.                        ios_base::app );
  102.  
  103.   // create an ostream object
  104.   ostream out_bis(in_bis.rdbuf());  
  105.  
  106.   // initialize the string-buffer with test_string
  107.   in_bis.str(test_string);
  108.  
  109.   out_bis << endl;
  110.  
  111.   // output the base info before each integer
  112.   out_bis << showbase;
  113.  
  114.   ostream::pos_type pos= out_bis.tellp();
  115.  
  116.   // output l in hex with a field with of 20 
  117.   out_bis << hex << setw(20) << l << endl;
  118.  
  119.   // output l in oct with a field with of 20
  120.   out_bis << oct << setw(20) << l << endl;
  121.  
  122.   // output l in dec with a field with of 20
  123.   out_bis << dec << setw(20) << l << endl;
  124.  
  125.   // output the all buffer
  126.   cout << in_bis.rdbuf();
  127.  
  128.   // seek the input sequence to pos  
  129.   in_bis.seekg(pos);
  130.  
  131.   int a,b,d;
  132.  
  133.   in_bis.unsetf(ios_base::basefield);
  134.  
  135.   // read the previous outputted integer
  136.   in_bis >> a >> b >> d;
  137.  
  138.   // output 3 times 20
  139.   cout << a << endl << b << endl << d << endl;
  140.  
  141.   return 0;
  142.  }
  143.  
  144.  
  145.  
  146.