#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
void formatted_output(std::istream & in, std::ostream& out, std::size_t maxCharsPerLine)
{
bool done = false;
unsigned charsInLine = 0;
std::string token;
while ( in >> token )
{
std::size_t newLineSize = charsInLine + token.size();
if (newLineSize <= maxCharsPerLine)
{
out << token;
if (newLineSize == maxCharsPerLine || newLineSize+1 == maxCharsPerLine)
{
out << '\n';
charsInLine = 0;
}
else
{
out << ' ';
charsInLine += token.length() + 1;
}
}
else
{
out << '\n';
out << token << ' ';
charsInLine = token.length() + 1;
}
}
out << '\n';
}
const std::string content =
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor "
"incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis "
"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu "
"fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in "
"culpa qui officia deserunt mollit anim id est laborum.";
int main()
{
// std::ifstream in("input.txt");
// if ( !in.is_open() )
// {
// // ... error handling here.
// }
std::istringstream in(content);
for (unsigned i = 1; i < 7; ++i)
std::cout << std::setw(10) << i;
std::cout << '\n';
std::cout << std::setfill('.');
for (unsigned i = 1; i < 7; ++i)
std::cout << std::setw(9) << "" << '0';
std::cout << '\n';
formatted_output(in, std::cout, 60);
}