Formatted and File Input/Output - Chapter 3 part 2

File I/O

In CS 110B or 111B you'll define your own classes and abstract data types. But for file i/o, we use some pre-defined classes. So we need a few definitions:

class - a datatype to define an object.
object - a variable storing an instance of a class. An object is a special kind of variable that is like a package containing many things inside. It has multiple values inside it, and it also can have functions as part of it. These functions are called member functions.

To work with file i/o we need to include the fstream library with the statement:
  #include <fstream>
This allows us access to the ifstream class for input from a file, and the ofstream class for output to a file. We declare an input file stream object with the following statement:
  ifstream file_in;
Then we open the file:
  file_in.open("scorefile.txt"); // Your filename here

Once a file stream is open we use it in much the same way as we use cin and cout. So to input a value from a file that is whitespace-delimited, we write:
  double one_score;
  file_in >> one_score;

Or to input the next character including whitespace or anything else, we write:
  char c;
  file_in.get(c);

Similar to the ifstream class, the ofstream class is used for file output.

For greater control over the format of file or screen output, use the library iomanip. This gives you access to functions to control decimal precision, output width and alignment, etc. Here is a sample program inputting three numbers and outputting them in a formatted file:
  #include <iostream>
  #include <fstream>
  #include <iomanip>
  using namespace std;

  int main()
  {
    ofstream out_stream;
    double num1, num2, num3;

    cout << "Please enter three numbers separated by spaces: ";
    cin >> num1 >> num2 >> num3;

    // create and open a file called "output_file.txt" for output:
    out_stream.open("output_file.txt");

    // standard "fixed" notation instead of scientific notation,
    // also making "precision" below set number of digits after
    // the decimal point, not significant digits:
    out_stream << fixed;

    // show all specified digits after decimal, even if 0's:
    out_stream << showpoint;

    // show 2 digits after decimal for floating pt. numbers:
    out_stream << setprecision(2);

    // Output num1 in a width of 6 characters
    // (right-aligned by default):
    out_stream << setw(6) << num1 << endl;
    // and the same with num2 and num3:
    out_stream << setw(6) << num2 << endl;
    out_stream << setw(6) << num3 << endl;

    // close the output file:
    out_stream.close();

    return 0;
  }

  /* Sample Output:

  bash-2.04$ a.out
  Please enter three numbers separated by spaces: 1 25.2 198.87634
  bash-2.04$ cat output_file.txt
    1.00
   25.20
  198.88

  */

Inputting an Entire Line of Text

In-class exercise: Write a program that has the user enter their full name (first and last name, with spaces) into a single string variable. Then the user should enter 3 numeric homework grades. The program should output the name and the average of the grades, formatted to show just 1 digit after the decimal point. Make sure to use a floating point data type for the grades.

nameAverage.cpp: Solution to above in-class exercise averaging grades