The document provides an overview of file handling in C++ using the fstream library, detailing the use of ifstream, ofstream, and fstream data types for file input and output. It explains how to read from and write to files using operators and member functions, including getline and character I/O methods. Additionally, it discusses file access flags and the ability to work with multiple files simultaneously.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
16 views16 pages
File Handling
The document provides an overview of file handling in C++ using the fstream library, detailing the use of ifstream, ofstream, and fstream data types for file input and output. It explains how to read from and write to files using operators and member functions, including getline and character I/O methods. Additionally, it discusses file access flags and the ability to work with multiple files simultaneously.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16
Using Files
1. Requires fstream header file
– use ifstream data type for input files – use ofstream data type for output files – use fstream data type for both input, output files 2. Can use >>, << to read from, write to a file 3. Can use eof member function to test for end of input file
• fstream object can be used for either input or output
• Must specify mode on the open statement • Sample modes: ios::in – input ios::out – output • Can be combined on open call: dFile.open("class.txt", ios::in | ios::out);
Member Functions for Reading and Writing Files • Functions that may be used for input with whitespace, to perform single character I/O, or to return to the beginning of an input file • Member functions: getline: reads input including whitespace get: reads a single character put: writes a single character
The getline Function • Three arguments: – Name of a file stream object – Name of a string object – Delimiter character of your choice – Examples, using the file stream object myFile, and the string objects name and address: getline(myFile, name); getline(myFile, address, '\t');