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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /***************************************************************************
  4.  *
  5.  * ifstream.cpp - basic_ifstream example
  6.  *
  7.  * $Id: ifstream.cpp,v 1.6 1996/09/26 22:47:38 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. //
  45. // stdlib/examples/manual/ifstream.cpp
  46. //
  47. #include<iostream>
  48. #include<fstream>
  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.   char   *ntbs="Le minot passait la piece a frotter";
  59.   char   c;
  60.   char   buf[50];
  61.  
  62. #ifndef _RWSTD_NO_EXCEPTIONS
  63. try {
  64. #endif
  65.  
  66.   // create a read/write file-stream object on char
  67.   // and attach it to an ifstream object
  68.   ifstream in("ifstream.out",ios_base::in | ios_base::out |
  69.               ios_base::trunc);
  70.  
  71.   if ( !in.is_open() ) 
  72. #ifndef _RWSTD_NO_EXCEPTIONS
  73.     throw(ios_base::failure("Open error"));
  74. #else
  75.     abort();
  76. #endif
  77.  
  78.   // tie the ostream object to the ifstream object
  79.   ostream out(in.rdbuf());   
  80.  
  81.   // output ntbs in out
  82.   out << ntbs << endl;
  83.  
  84.   // seek to the beginning of the file
  85.   in.seekg(0);
  86.  
  87.   // output each word on a separate line
  88.   while ( in.get(c) )
  89.    {
  90.      if ( ifstream::traits_type::eq(c,' ') ) 
  91.       cout << endl;
  92.      else
  93.       cout << c;
  94.    }
  95.   cout << endl << endl;
  96.  
  97.   // move back to the beginning of the file
  98.   in.seekg(0); 
  99.  
  100.   // clear the state flags
  101.   in.clear();
  102.  
  103.   // does the same thing as the previous code
  104.   // output each word on a separate line
  105.   while ( in >> buf )
  106.    cout << buf << endl; 
  107.     
  108.   cout << endl << endl;
  109.  
  110.   // output the base info before each integer
  111.   out << showbase;
  112.  
  113.   ostream::pos_type pos= out.tellp();
  114.  
  115.   // output l in hex with a field with of 20 
  116.   out << hex << setw(20) << l << endl;
  117.  
  118.   // output l in oct with a field with of 20
  119.   out << oct << setw(20) << l << endl;
  120.  
  121.   // output l in dec with a field with of 20
  122.   out << dec << setw(20) << l << endl;
  123.  
  124.   // move back to the beginning of the file
  125.   in.seekg(0);
  126.  
  127.   // output the all file
  128.   cout << in.rdbuf();
  129.  
  130.   // clear the flags 
  131.   in.clear(); 
  132.  
  133.   // seek the input sequence to pos  
  134.   in.seekg(pos);
  135.   
  136.   int a,b,d;
  137.  
  138.   in.unsetf(ios_base::basefield);
  139.  
  140.   // read the previous outputted integer
  141.   in >> a >> b >> d;
  142.  
  143.   // output 3 times 20
  144.   cout << a << endl << b << endl << d << endl;
  145.  
  146. #ifndef _RWSTD_NO_EXCEPTIONS
  147. }
  148.   catch( ios_base::failure var )
  149.    {
  150.       cout << var.what();
  151.    }
  152. #endif   
  153.  
  154.   return 0;
  155. }
  156.  
  157.  
  158.  
  159.  
  160.