How to easily indent output to ofstream?

This is the perfect situation to use a facet. A custom version of the codecvt facet can be imbued onto a stream. So your usage would look like this: int main() { /* Imbue std::cout before it is used */ std::ios::sync_with_stdio(false); std::cout.imbue(std::locale(std::locale::classic(), new IndentFacet())); std::cout << “Line 1\nLine 2\nLine 3\n”; /* You must imbue a … Read more

Read file line by line using ifstream in C++

First, make an ifstream: #include <fstream> std::ifstream infile(“thefile.txt”); The two standard methods are: Assume that every line consists of two numbers and read token by token: int a, b; while (infile >> a >> b) { // process pair (a,b) } Line-based parsing, using string streams: #include <sstream> #include <string> std::string line; while (std::getline(infile, line)) … Read more