Professional Documents
Culture Documents
OUTPUT
UCCD1004 Programming Concepts and Practices
Content Overview
Structure of C++ Programs
• It inputs the next number typed on the keyboard and stores that
number in payRate.
• It is equivalent to
cin >> a;
cin >> b;
The extraction operator >>
cout, <<, and endl.
• setprecision(n)
#include <iomanip>
cout << setprecision(2) << 3.142;
ifstream ofstream
Can use input file object and >> to copy Can use output file object and << to write
data from file to variables data to a file
3-16
Flags of fstream
ifstream inFile;
ofstream outFile;
Flag Description Example
ios::in Open for input operations; inFile.open
default operation of ifstream. ("example.txt",ios::in);
return 0;
}
To append to a file
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open ("example.txt",ios::app);
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Member Functions for Reading and Writing Files
• Unlike the extraction operator >>, these reading functions do not skip
whitespace:
– getline: reads input including whitespace
string line;
getline (myfile,line)
3-28
Common issue with read and write files