Skip to main content

12.1) File Streams


  • File input/output uses streams, just like console input/output
  • There are three relevant stream types,made available via #include <fstream>
    • ifstream, representing an input file
    • ofstream, representing an output file
    • fstream, representing a file used for both input and output
  • After creating one of those stream objects, I/O can be done just as it is with cin and cout
  • If dealing with text files
    • Use the >> operator to read values from the stream
    • Use the << operator to write values to the stream
  • If dealing with binary files (not covered here)
    • Use the read method to read bytes from the stream
    • Use the write method to write bytes to the stream