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

  1. #include "stlexam.h"
  2. #pragma hdrstop
  3. /***************************************************************************
  4.  *
  5.  * istream1.cpp - istream example
  6.  *
  7.  * $Id: istream1.cpp,v 1.6 1996/09/26 22:58:33 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/istream1.cpp
  45. //
  46. #include<iostream>
  47. #include<istream>
  48. #include<fstream>
  49.  
  50. #ifndef _RWSTD_NO_NAMESPACE
  51.   using namespace std;
  52. #endif 
  53.  
  54. int main ( )
  55. {
  56.  
  57.   float f= 3.14159;
  58.   int   i= 3;
  59.   char  s[200];
  60.  
  61.   // open a file for read and write operations 
  62.   ofstream out("example", ios_base::in | ios_base::out
  63.                | ios_base::trunc);
  64.  
  65.   // tie the istream object to the ofstream filebuf 
  66.   istream  in (out.rdbuf());
  67.  
  68.   // output to the file
  69.   out << "Annie is the Queen of porting" << endl;
  70.   out << f << endl;
  71.   out << i << endl;
  72.   
  73.   // seek to the beginning of the file
  74.   in.seekg(0);
  75.   
  76.   f = i = 0;
  77.  
  78.   // read from the file using formatted functions
  79.   in >> s >> f >> i;
  80.  
  81.   // seek to the beginning of the file
  82.   in.seekg(0,ios_base::beg);
  83.  
  84.   // output the all file to the standard output
  85.   cout << in.rdbuf();
  86.  
  87.   // seek to the beginning of the file
  88.   in.seekg(0);
  89.  
  90.   // read the first line in the file
  91.   // "Annie is the Queen of porting"
  92.   in.getline(s,100);
  93.  
  94.   cout << s << endl;
  95.  
  96.   // read the second line in the file
  97.   // 3.14159
  98.   in.getline(s,100);
  99.  
  100.   cout << s << endl;
  101.  
  102.   // seek to the beginning of the file
  103.   in.seekg(0);
  104.  
  105.   // read the first line in the file
  106.   // "Annie is the Queen of porting"
  107.   in.get(s,100);
  108.  
  109.   // remove the newline character
  110.   in.ignore();
  111.  
  112.   cout << s << endl;
  113.  
  114.   // read the second line in the file
  115.   // 3.14159
  116.   in.get(s,100);
  117.  
  118.   cout << s << endl;
  119.  
  120.   // remove the newline character
  121.   in.ignore();
  122.  
  123.   // store the current file position   
  124.   istream::pos_type position = in.tellg();
  125.  
  126.   out << "replace the int" << endl;
  127.  
  128.   // move back to the previous saved position
  129.   in.seekg(position);   
  130.  
  131.   // output the remain of the file
  132.   // "replace the int"
  133.   // this is equivalent of
  134.   // cout << in.rdbuf();
  135.   while( !ifstream::traits_type::eq_int_type( in.peek(),
  136.                                               ifstream::traits_type::eof()) )
  137.    cout << ifstream::traits_type::to_char_type(in.get()); 
  138.  
  139.   cout << "\n\n\n" << flush;
  140.  
  141.   return 0;
  142. }
  143.  
  144.  
  145.  
  146.  
  147.