Lesson 5.1

Cards (7)

  • Writing to a File

    Write information to a file using the stream insertion operator (<<) with an ofstream or fstream object
  • Header files <iostream> and <fstream> must be included in C++ source file for file processing
  • fstream
    • Defines three new data types: ofstream, ifstream, fstream
    • ofstream: Represents the output file stream for creating and writing information to files
    • ifstream: Represents the input file stream for reading information from files
    • fstream: Represents the file stream generally, with capabilities of both ofstream and ifstream for creating files, writing information, and reading information
  • close() function is used to close a file
  • Standard syntax for open() function: void open(const char *filename, ios::openmode mode)
  • Reading from a File
    Read information from a file using the stream extraction operator (>>) with an ifstream or fstream object
  • Opening a File
    1. A file must be opened before reading from or writing to it
    2. Either ofstream or fstream object is used to open a file for writing
    3. ifstream object is used to open a file for reading