home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / examples / streams / fstream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  2.9 KB  |  94 lines

  1. #include "fstream.h"
  2.  
  3. // Example using an fstream (Input/Output File-based stream)
  4. int main(void)
  5. {
  6.    // Declare an fstream object called "mystream" and initialize
  7.    // it to perform I/O to the file "myiofile.dat"
  8.    fstream mystream("myiofile.dat", ios::in|ios::out);
  9.  
  10.    // Declare an unopened fstream object called "mystream2"
  11.    fstream mystream2;
  12.  
  13.    // Declare a pointer to an fstream object
  14.    fstream *stream_p;
  15.  
  16.    int i;
  17.  
  18.    if(!mystream)
  19.    {
  20.       cout << "Error opening file \"myiofile.dat\"!" << endl;
  21.       return 20;
  22.    }
  23.  
  24.    // Read an integer from the file attached to "mystream"
  25.    mystream >> i;
  26.  
  27.    if(!mystream) cout << "Read from \"myiofile.dat\" failed" << endl;
  28.    else cout << "Read " << i << " from \"myiofile.dat\"" << endl;
  29.  
  30.    i = i + 1;                     // Add one to the integer
  31.    mystream.seekp(0,ios::beg); // Seek back to beginning of file
  32.  
  33.    mystream << i;                 // Write the integer back
  34.  
  35.    if(!mystream) cout << "Write to \"myiofile.dat\" failed" << endl;
  36.    else cout << "Wrote " << i << " to \"myiofile.dat\"" << endl;
  37.  
  38.    // Initialize the unopened "mystream2" stream to perform I/O to
  39.    // the file "myiofile2.dat"
  40.    mystream2.open("myiofile2.dat", ios::app|ios::in|ios::out);
  41.  
  42.    if(!mystream2)
  43.    {
  44.       cout << "Error opening file \"myiofile2.dat\"!" << endl;
  45.       return 20;
  46.    }
  47.  
  48.    // Read an integer from "myiofile2.dat"
  49.    mystream2 >> i;
  50.  
  51.    if(!mystream2) cout << "Read from myiofile2.dat failed" << endl;
  52.    else cout << "Read " << i << " from myiofile2.dat" << endl;
  53.  
  54.    // Write the new integer.  Note that this will APPEND the new
  55.    // integer to the old file, not replace the old file, since
  56.    // we did not seek to the beginning of the file before writing.
  57.    // Put a blank in to seperate the old integer from the new one.
  58.    i = i + 1;
  59.    mystream2 << " " << i;
  60.  
  61.    if(!mystream2) cout << "Write to myiofile2.dat failed" << endl;
  62.    else cout << "Appended " << i << " to myiofile2.dat" << endl;
  63.  
  64.    // Allocate a new fstream using "new" and use it to perform I/O to
  65.    // the file "myiofile3.dat"
  66.    stream_p = new fstream("myiofile3.dat", ios::in|ios::out);
  67.  
  68.    if(!stream_p || !*stream_p)
  69.    {
  70.       cout << "Error opening file \"myiofile3.dat\"!" << endl;
  71.       return 20;
  72.    }
  73.  
  74.    *stream_p >> i;
  75.  
  76.    if(!*stream_p) cout << "Read from myiofile3.dat failed" << endl;
  77.    else cout << "Read " << i << " from myiofile3.dat" << endl;
  78.  
  79.    i = i + 1;
  80.    stream_p->seekp(0, ios::beg);  // Rewrite this one, not append
  81.    *stream_p << i;
  82.  
  83.    if(!*stream_p) cout << "Write to file myiofile3.dat failed" << endl;
  84.    else cout << "Wrote " << i << " to myiofile3.dat" << endl;
  85.    
  86.    // Free the object just allocated.  This will call the destructor
  87.    // for the stream and therefore close the file.
  88.    delete stream_p;
  89.  
  90.    // Destructors for the other streams will automatically be called.
  91.    return 0;
  92. }
  93.  
  94.