home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C18 / Breakup.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.9 KB  |  67 lines

  1. //: C18:Breakup.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. // Breaks a file up into smaller files for 
  7. // easier downloads
  8. #include "../require.h"
  9. #include <iostream>
  10. #include <fstream>
  11. #include <iomanip>
  12. #include <strstream>
  13. #include <string>
  14. using namespace std;
  15.  
  16. int main(int argc, char* argv[]) {
  17.   requireArgs(argc, 1);
  18.   ifstream in(argv[1], ios::binary);
  19.   assure(in, argv[1]);
  20.   in.seekg(0, ios::end); // End of file
  21.   long fileSize = in.tellg(); // Size of file
  22.   cout << "file size = " << fileSize << endl;
  23.   in.seekg(0, ios::beg); // Start of file
  24.   char* fbuf = new char[fileSize];
  25.   require(fbuf != 0);
  26.   in.read(fbuf, fileSize);
  27.   in.close();
  28.   string infile(argv[1]);
  29.   int dot = infile.find('.');
  30.   while(dot != string::npos) {
  31.     infile.replace(dot, 1, "-");
  32.     dot = infile.find('.');
  33.   }
  34.   string batchName(
  35.     "DOSAssemble" + infile + ".bat");
  36.   ofstream batchFile(batchName.c_str());
  37.   batchFile << "copy /b ";
  38.   int filecount = 0;
  39.   const int sbufsz = 128;
  40.   char sbuf[sbufsz];
  41.   const long pieceSize = 1000L * 100L;
  42.   long byteCounter = 0;
  43.   while(byteCounter < fileSize) {
  44.     ostrstream name(sbuf, sbufsz);
  45.     name << argv[1] << "-part" << setfill('0') 
  46.       << setw(2) << filecount++ << ends;
  47.     cout << "creating " << sbuf << endl;
  48.     if(filecount > 1) 
  49.       batchFile << "+";
  50.     batchFile << sbuf;
  51.     ofstream out(sbuf, ios::out | ios::binary);
  52.     assure(out, sbuf);
  53.     long byteq;
  54.     if(byteCounter + pieceSize < fileSize)
  55.       byteq = pieceSize;
  56.     else
  57.       byteq = fileSize - byteCounter;
  58.     out.write(fbuf + byteCounter, byteq);
  59.     cout << "wrote " << byteq << " bytes, ";
  60.     byteCounter += byteq;
  61.     out.close();
  62.     cout << "ByteCounter = " << byteCounter 
  63.       << ", fileSize = " << fileSize << endl;
  64.   }
  65.   batchFile << " " << argv[1] << endl;
  66. } ///:~
  67.