Wrapping Lines in a Text File
Problem
You want to "wrap" text at a specific number of characters in a file. For example, if you want to wrap text at 72 characters, you would insert a new-line character after every 72 characters in the file. If the file contains human-readable text, you probably want to avoid splitting words.
Solution
Write a function that uses input and output streams to read in characters with istream::get(char), do some bookkeeping, and write out characters with ostream::put(char). Example 4-25 shows how to do this for text files that contain human-readable text without splitting words.
Example 4-25. Wrapping text
#include #include #include #include #include #include using namespace std; void textWrap(istream& in, ostream& out, size_t width) { string tmp; char cur = '