previous | start | next

## Reading and Writing Text Files

- You read in a single character with a get method.

  >     char ch;
  >     input_data.get(ch);

- If you read a character and you regretted it, you can unget it, so that the next input operation can be read again.

  >     char ch;
  >     input_data.get(ch);
  >     if ('0' <= ch && ch <= '9') /* it was a digit */
  >     {
  >        input_data.unget(); /* oops - didn't want to read it */
  >        int n;
  >        input_data >> n; /* read integer starting with ch */
  >     }

- This is called one-character lookahead: at the next character in the input stream you can make a decision what you want to read in next, but not more than one character.
- Older implementations of the stream library do not have the `unget` member function - use `input_data.put_back(ch)` instead.
- When you are done using a file, you should close it.

  >     input_data.close();

previous | start | next