I/O Streams

I/O

Overview of iostream, the main C++ Library for input and output:

#include <iostream>

using std::cin; // default input stream
using std::cout; // default output stream
using std::endl; // end of line, flushes buffer
using std::cerr; //default error output stream

cout

cout << "Hello world!" << endl;

There are no format specifier (%d, %s, etc.). Items that need to be printed are arranged in printing order, which is easier to read and understand. However, if you still want to use it, it does work after including <cstdio>.

cin

string name;
cin >> name;

If you input an entire sentence, it will still read one token at a time (word by word per iteration). This code will store the smallest word in an inputted sentence and print it out.

string word, smallest;
while (cin >> word) {
	if(smallest.empty() || word < smallest) {
		samllest = word;
	}
}
count << smallest < endl;
cin.get(ch)

Chaining

In the above example, we send two things to cout, this is called chaining. The insert operator joins all the items to write in a "chain".
The leftmost item in the chain is the stream being written to.

cout << "We have " << inventory << " " << item << "s left." << endl;

inventory and item are variables.

Operator Overloading

<< usually does bitwise left-shift; but if operand on the left is a C++ stream (cout), << is the insert operator. Shifting only applies to numbers.
More on this later.

File I/O

In C we used fprintf and fscanf to read and write from files. In C++, std::ofstream and std::instream are their counterparts.
These are defined in the file-stream header: #include <fstream> and still use the << and >> operators.

Examples

ofstream:

#include <iostream>
#include <fstream>
int main() {
	std::ofstream ofile("hello.txt");
	ofile << "Hello world!" << std::endl;
	return 0;
}

This program writes "Hello world!" to a text file named "hello.txt".

ifstream:

#include <iostream>
#include <fstream>
#inclue <string>
int main() {
	std::ifstream ifile("hello.txt");
	std::string word;
	while( ifile >> word )
		std::cout << word <<std::endl;
	return 0;
}

This program will print

Hello,
World!

I/O from/to strings

std::stringstream

Instead of reading or writing to console or file, it reads and writes to a temporary string ("buffer") stored inside. Useful for programs that process strings, like one that parses input.

#include <iostream>
#include <sstream>

int main() {
	std:stringstream ss; // create object
	ss << "Hello, world!"" << std::endl;
	std::cout << ss.str();
	return 0;
}

stringstream details

stringstream also comes in flavors that only do reading or writing:

example

int num;
std::string word1, word2;

ss << "Hello" << ' ' << 2019 << " world";
ss >> word1 >> num >> word2;

This will store Hello into word1, 2019 into num, and world into word2.