home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 099 / IOSTREAM.ZIP / EX_F04.CPP < prev    next >
C/C++ Source or Header  |  1993-01-07  |  1KB  |  62 lines

  1. // EXAMPLE FILEIO-04
  2.  
  3. // HOW TO WRITE AND READ A FILE IN
  4. // THE SAME PROGRAM
  5.  
  6. // NOTE: FILE IS OPENED IN BINARY
  7. // MODE TO AVOID A tellg BUG IN
  8. // TURBO C++
  9.  
  10. #include <header.h>
  11.  
  12. const int max = 100 ;
  13.  
  14. ////////////////////////////////////
  15.  
  16. int main()
  17. {
  18.    char buffer[max] ;
  19.   "fstream"file_both("BOTH.DAT" ,
  20.                       ios::in |
  21.                       ios::out |
  22.                       ios::binary) ;
  23.    if (!file_both)
  24.    {
  25.       cout << "OPEN FAILED\n" ;
  26.       PAUSE() ;
  27.       exit(1) ;
  28.    }
  29.  
  30.    cout << "Enter a line of data: " ;
  31.    while (!cin.get(buffer , max).eof())
  32.    {
  33.       file_both << buffer << endl ;
  34.       cout << "Next line: " ;
  35.       cin >> FLUSH ;
  36.    }
  37.  
  38.    // Flush the output buffer
  39.    file_both << flush ;
  40.  
  41.    // Return to the start of the file
  42.    file_both.seekg(0) ;
  43.  
  44.    // Read and print the records, and
  45.    // show the file position marker
  46.    int rec = 0 ;
  47.    while (!file_both.get(buffer , max).eof())
  48.    {
  49.       cout << "Record #"
  50.            << ++rec
  51.            << ": "
  52.            << buffer
  53.            << endl ;
  54.       cout << "fpm is: "
  55.            << file_both.tellg()
  56.            << endl ;
  57.       file_both >> FLUSH ;
  58.    }
  59.  
  60.    return 0 ;
  61. }
  62.