Stdio and Streams
std::getline
- We can get the entire line with
std::getline
std::string line;
std::getlinecin, line;
Note that getline
also works for other input streams, such as cout
and cerr
stdout
- Output formatting: http://www.cplusplus.com/reference/iomanip/?kw=iomanip
- You can print out numbers in hex, e.g
std::cout << std::hex << 95 << std::endl
which will output5f
stdin
cin
std::string foo, bar;
std::cin >> foo >> bar;
grabs the next two "words" or strings from stdin which are separated by a whitespace character.
Variable Length Input
Options:
std::cin.eof()
std::cin.fail()
which return true ifstd::cin.eof()
is true OR some other problem occurs- The shortcut for
std::cin.fail()
is to checkstd::cin
as a boolean
File Streams
To use, #include <fstream>
- We can connect a stream to a filename:
std::ifstream input {"input.txt"};
- Also output stream:
std::ofstream output; output.open("output.txt");
- Remember to always close streams when done:
myStream.close()
Then we can do something like myFileStream >> "hello!"