$$ \newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\dx}{\text{ dx}}
\newcommand{\rang}{\text{rang}}
\newcommand{\s}{\ \ \ \ \ \ }
\newcommand{\arrows}{\s \Leftrightarrow \s}
\newcommand{\Arrows}{\s \Longleftrightarrow \s}
\newcommand{\arrow}{\s \Rightarrow \s}
\newcommand{\c}{\bcancel}
\newcommand{\v}[2]{
\begin{pmatrix}
#1 \\
#2 \\
\end{pmatrix}
}
\newcommand{\vt}[3]{
\begin{pmatrix}
#1 \\
#2 \\
#3 \\
\end{pmatrix}
}
\newcommand{\stack}[2]{
\substack{
#1 \\
#2
}
}
\newcommand{\atom}[3]{
\substack{
#1 \\
#2
}
\ce{#3}
}
$$
C++ Streams
Write to File
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open()) {
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Read file token by token
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
ifstream myfile ("example.txt");
if (myfile.is_open()) {
while (myfile >> temp) {
cout << temp << ' ';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Read file line by line
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open()) {
while ( getline (myfile, line) ) {
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}