Listing 2

//textgen.cpp
//creates random ASCII text files for testing purposes

#include <stdio.h>
#include <stream.hpp>
#include <stdlib.h>

main()
{
   char ch, filename[13];
   int p, q = 0, nblanks = 0;
   unsigned long i, j = 1, nchar;

   cout << "Type name for testfile\n";
   cin >> filename;
   cout << "Type number of characters including whitespace\n";
   cin >> nchar;

   char *text = new char[nchar-1];
   if (!text) cout << "Out of heap memory\n";
   filebuf file1;
   file1.open(filename,output);
   ostream output_file(&file1);

   for (i = 0; i < nchar; i++){
      p = 33 + (94.0 * rand() / 32768.0);
              //generate random numbers between 33 and 126
      if (i == j){         //create words 1-10 chars long
         p = 32;
         q = 1 + (11.0 * rand() / 32768.0);
         j = j + q;
         ++nblanks;
         }
      text[i] = p;
      cout << text[i];
      output_file << text[i];
      }
   cout << "\n" << "nblanks = " << nblanks;
   delete text;
}

/* End of File */