home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C11 / Linenum.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  843 b   |  33 lines

  1. //: C11:Linenum.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Add line numbers
  7. #include "../require.h"
  8. #include <fstream>
  9. #include <strstream>
  10. #include <cstdlib>
  11. using namespace std;
  12.  
  13. int main(int argc, char* argv[]) {
  14.   requireArgs(argc, 1, "Usage: linenum file\n"
  15.     "Adds line numbers to file");
  16.   strstream text;
  17.   {
  18.     ifstream in(argv[1]);
  19.     assure(in, argv[1]);
  20.     text << in.rdbuf(); // Read in whole file
  21.   } // Close file
  22.   ofstream out(argv[1]); // Overwrite file
  23.   assure(out, argv[1]);
  24.   const int bsz = 100;
  25.   char buf[bsz];
  26.   int line = 0;
  27.   while(text.getline(buf, bsz)) {
  28.     out.setf(ios::right, ios::adjustfield);
  29.     out.width(2);
  30.     out << ++line << ") " << buf << endl;
  31.   }
  32. } ///:~
  33.