previous | start | next

## Reading and Writing Text Files

- To access a disk file, you need to open a file variable.
  - type `ifstream` for input

    >     ifstream input_data;

  - type `ofstream` for output
  - type `fstream` for both input and output
- To read anything from a file, you need to open it.

  >     input_data.open("input.dat");

- To read from the file, use the name of the `ifstream` variable in place of `cin`.

  >     int n;
  >     double x;
  >     input_data >> n >> x;

- You read in strings in the usual way.

  >     string s;
  >     input_data >> s; /* read a word */
  >     getline(input_data, s); /* read a line */

previous | start | next