\( % theorem \newenvironment{pf}{\textbf{Proof.}}{\rule{1ex}{1ex}} \newtheorem{qu}{Question} \newtheorem{thm}{Theorem} \newtheorem{rmk}{Remark} \newtheorem{go}{Goal} \newtheorem{df}{Definition} \newtheorem{prop}{Proposition} \newtheorem{mot}{Motivation} \newtheorem{ex}{Example} \lstnewenvironment{code}{}{} \newcommand{\nc}{\newcommand} %% env \nc{\env}[2]{ \begin{#1} #2 \end{#1} } \nc{\arr}[2]{ \begin{array}{#1} #2 \end{array} } \nc{\arrb}[2]{ \left\{ \begin{array}{#1} #2 \end{array} \right. } \nc{FTC}[3]{ \left[#3\right]^{#2}_{#1} } \nc{\bracs}[1]{ \left[#1\right] } \nc{\brac}[1]{ \left(#1\right) } \nc{\bracp}[1]{ \left\{#1\right\} } \nc{\intd}[4]{\int^{#2}_{#1}\left(#3\right) #4} % shortcut \nc{\Lra}{\Leftrightarrow} \nc{\Ra}{\Rightarrow} \nc{\La}{\Leftarrow} \nc{\C}{\mathbb{C}} \nc{\R}{\mathbb{R}} \nc{\Fc}{\mathcal{C}} \nc{\bds}{\boldsymbol} \nc{\ds}{\displaystyle} % begin end \nc{\bit}{\begin{itemize}} \nc{\eit}{\end{itemize}} \nc{\bcode}{\begin{code}} \nc{\ecode}{\end{code}} \nc{\bsage}{\begin{sageblock}} \nc{\esage}{\end{sageblock}} %pic \nc{\pic}{\includegraphics} \nc{\svg}{\includesvg} \nc{\pica}{\includegraphics[width=100px,height=100px]} \nc{\picb}{\includegraphics[width=150px,height=150px]} \nc{\vs}{\vspace} \nc{\tbf}{\textbf} %arrow \nc{\upa}{\nearrow} \nc{\downa}{\searrow} \nc{\upw}{\rcurvearrowright} \nc{\downw}{\curvearrowright} \)

Input and output

The C++ standard library provides type istream for input and type ostream for output.

Input   <------------------------> Output
istream          iostream          ostream
keyboard                           screen

For example, cin is the standard input stream which is an object of istream and cout is the standard output steam which is an object of ostream.

int a=0;
cin>>a;
cout<<a;

overloading >>

We overloaded >> for Date type.

ostream & operator<<(ostream & os, const Date & d){
    os<<d.month()<<"-"<<d.day()<<", "<<d.year();
    return os;
}
//
Date d {2016,Month::feb,10};
cout<<d;

As you can see above, we work with os which is an object of ostream. cout corresponds to os here.

Check.

10.3 Files

Files     <-- I/O system    --> Main memory
sequence                        Objects 
of bytes                        of types
          <- serialization(ORM)
          deserialzation  ----> 

ifstream        fstream         ofstream
Read                            Write

For serialization, check https://en.wikipedia.org/wiki/Serialization

Process of reading/writing a file

Reading

  1. Know its name
  2. Open it for reading
  3. Read in the characters
  4. Close it

Writing

  1. Name it.
  2. Open or create a new file.
  3. Write out our objects.
  4. Close it.

10.4 Open a file

Read.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//
cout << "File name:";
string name;
cin >> name;
ifstream myfile {name}; //Read
vector<double> data;
if(myfile){
    for(double x;myfile>>x;){
        data.push_back(x);
    }
}
else cerr<<"Can't open "<<name;

Run

Check: Show the sum of data from a file.

Code!

Write

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//
cout << "File name:";
string name;
cin >> name;
ofstream myfile {name};// open or create to write.
vector<double> data={1,2,3,4};
if(myfile){
    for(double x : data ){
        myfile<<x<<endl;//Write
    }
}
else cerr<<"Can't open "<<name;

Run

Check: Get usernames and store them to users.txt file.

To stop typing, just type an enter.

for(string user="";;){
    getline(cin, user);// We need to use a getline to handle an enter.
    if (user.length() == 0) break;
    else users.push_back(user);
}

Code!