String IO

#include <iostream>
using namespace std;
int main(){
	int x;
	cout << "Hello world"" endl;
	cin >> x;
	return 0;
}

String Streams

Helpful when inputs are line oriented, i.e., user will enter a line and a program has to check if inputs on that line are correct. Can be used to extract inputs in the line

#include <sstream>
#include <string>

int main(){
	int ID;
	string name;
	string inputline = "1001 Joe";
	stringstream myStringStream(inputLine); //create a string stream initialized with inputLine
	stringstream >> ID; //input into ID
	myStringsStream >> name; //input into name
	cout << "Name: " << name << endl << "ID: " << ID << endl;
	myStringStream << name << ID;
	cout << myStringStream.str(); //converts a string stream to a string to print it 
	return 0;
}

File IO

#include <fstream>
using namespace std;
int main(){
	ofstream outfile("myFile.txt");
	string name = "We are engineers!";
	outfile << name;
	outfile.close();
}

Input from a file

#include <fstream>
using namespace std;
int main(){
	ifstream inputfile("myfile.txt");
	int num1, num2, num3;
	inputfile >> num1 >> num2 >> num3; //input from file
	inputfile.close();
	return 0;
}

File paths

Buffering