Justify a Text File, right- or left-justify text. - C++ File Stream

C++ examples for File Stream:Text File

Description

Justify a Text File, right- or left-justify text.

Demo Code

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char** argv) {
   ifstream in("main.cpp");
   ofstream out("target.txt");

   int w = 72;/*from  www.ja  v  a  2 s. c om*/
   if (argc == 4)
      w = atoi(argv[3]);

   string tmp;
   out.setf(ios_base::right);   // Tell the stream to right-justify
   while (!in.eof()) {
      out.width(w);                  // Reset width after
      getline(in, tmp, '\n');        // each write
      out << tmp << '\n';

   }
   out.close();
}

Related Tutorials