Source Code. Projects. Nerd Stuff. Art Stuff.

Lesson 4: Formatted and File Input/Output

Formatted and File Input/Output

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:

1"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;

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:"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:

    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

  • The Unix command “cat output_file.txt” above outputs the contents of the file on the screen.
  • Notice that spaces was added before the first two numbers, to fill the 6 character width specified by setw(6).
  • Remember that when you set the width, it will only affect the one output immediately following it. The output width always reverts back to 0 (normal output).
  • All of the above output stream manipulators can be used the same way with the cout object, to affect how screen output is displayed.

Inputting an Entire Line of Text

  • To get entire string (including whitespace) entered by user until they hit the return key, or until they enter a maximum number of characters, use getline: e.g. cin.getline(str, 80); (max 79 chars of input, allowing space for null character) For example:
        char class_title[81];
        cout << "Enter name of class";
        cin.getline(class_title, 81);
        // Exact user entry, up to newline or 80th character
        // will be stored in class_title. e.g. "Intro. to Programming in C++"
  • Similarly, you can use getline with file input streams.

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