MIS-307
Stream Input/Output with Files
Dr. Reithel


Stream operation mode. These parameters can be logically ORed. 

ios::open_mode 
enum open_mode  {
   app,       // Append data--always write at end of file.
   ate,       // Seek to end of file upon original open.
   in,        // Open for input (default for ifstreams).
   out,       // Open for output (default for ofstreams).
   binary,    // Open file in binary mode.
   trunc,     // Discard contents if file exists (default if out
              // is specified and neither ate nor app is specified).
   nocreate,  // If file does not exist, open fails.
   noreplace, // If file exists, open for output fails unless ate or app is set.
};



#include < fstream.h >
#include < iostream.h >

fstream  output_file;
char     filename[128];
char     nl;

int main()
{
  cout << "Enter a filename to open for output \n";
  cin >> filename;
  cin.get(nl);

  output_file.open(filename, ios::out);

  output_file << "This will be line one of the output file" << "\n";
  output_file << "This will be line two of the output file" << "\n";

  output_file.close();
}


#include < fstream.h >
#include < iostream.h >

fstream  input_file;
char     filename[128];
char     lineone[256];
char     linetwo[256];
char     nl;

int main()
{
  cout << "Enter a filename to open for input \n";
  cin >> filename;
  cin.get(nl);

  input_file.open(filename, ios::in);
  input_file >> lineone;
  input_file.get(nl);
  input_file >> linetwo;
  input_file.get(nl);
  input_file.close();

  cout << lineone << "\n";
  cout << linetwo << "\n";

}

Last Modified: Wednesday, 26-Feb-97 10:50:00 CDT
Copyright © 1997 University of Mississippi. All rights reserved.
Comments: reithel@bus.olemiss.edu